Well, that's what I did today...
Saturday, August 27, 2011
Thursday, August 25, 2011
Non sequential sequence
Every now and then I've wanted to go from 0 to n in almost a random fashion. One way to solve this problem is to create a list of all the numbers from 0 to n and then shuffle the list. This works for small numbers and is definitely random but what if you have a larger set of numbers and aren't so fussed about the randomness?
I was finally able to enter the right terms into my search engine to read up on linear feedback shift registers.
I had a quick peek around the net to see if there was one written in Java and came up empty handed. So I wrote one.
A quick & on the input to get the bits we were concerned with, >> to shift the bits and ^ the relevant bits to work out if I should stick a 1 on the high order bit.
Turns out that was a bit overkill, just needed to know if the amount of relevant bits was even or odd. If odd, add the high order bit (and shift the whole lot of course).
The interesting bit was the values for the taps based on the number of bits in the 'register' you want.
The Wikipedia article says that for a LFSR to cycle through all numbers, there needs to be an even number of taps, the set of taps needs to be relatively prime.
Not being up with the maths of that last bit, I delved deeper into Wikipedia.
Then I realised I had the tools to brute force this puppy. Create a number of LFSR with different tap values and see which ones cycle through all numbers!
This table starts to look like 3 is a good number to use, but it doesn't work for 8, 9, 10 and 11 bit registers. So unfortunately to get a good sequence of numbers, you need to know a good tap. I just haven't worked out a nice way to find out what they are.
Might as well include a table of the numbers generated by the different taps. I'll use a 5 bit register because of the multiple taps.
I was finally able to enter the right terms into my search engine to read up on linear feedback shift registers.
I had a quick peek around the net to see if there was one written in Java and came up empty handed. So I wrote one.
A quick & on the input to get the bits we were concerned with, >> to shift the bits and ^ the relevant bits to work out if I should stick a 1 on the high order bit.
Turns out that was a bit overkill, just needed to know if the amount of relevant bits was even or odd. If odd, add the high order bit (and shift the whole lot of course).
if(Integer.bitCount((input & trap) ^ trap) % 2 == 1) { return (input >> 1) | (1 << bits - 1); } else { return input >> 1; }
The interesting bit was the values for the taps based on the number of bits in the 'register' you want.
The Wikipedia article says that for a LFSR to cycle through all numbers, there needs to be an even number of taps, the set of taps needs to be relatively prime.
Not being up with the maths of that last bit, I delved deeper into Wikipedia.
Then I realised I had the tools to brute force this puppy. Create a number of LFSR with different tap values and see which ones cycle through all numbers!
Size | Taps |
---|---|
3 | 11 101 |
4 | 11 1001 |
5 | 11 1001 1111 10111 11011 11101 |
6 | 11 11011 100001 100111 101101 110011 |
Might as well include a table of the numbers generated by the different taps. I'll use a 5 bit register because of the multiple taps.
101 | 16 | 8 | 4 | 18 | 9 | 20 | 26 | 13 | 6 | 19 | 25 | 28 | 30 | 31 | 15 | 7 | 3 | 17 | 24 | 12 | 22 | 27 | 29 | 14 | 23 | 11 | 21 | 10 | 5 | 2 | 1 |
1001 | 16 | 8 | 20 | 10 | 21 | 26 | 29 | 14 | 23 | 27 | 13 | 6 | 3 | 17 | 24 | 28 | 30 | 31 | 15 | 7 | 19 | 25 | 12 | 22 | 11 | 5 | 18 | 9 | 4 | 2 | 1 |
1111 | 16 | 8 | 20 | 26 | 13 | 22 | 11 | 21 | 10 | 5 | 2 | 17 | 24 | 28 | 14 | 23 | 27 | 29 | 30 | 31 | 15 | 7 | 19 | 9 | 4 | 18 | 25 | 12 | 6 | 3 | 1 |
10111 | 16 | 24 | 28 | 14 | 7 | 19 | 25 | 12 | 22 | 27 | 29 | 30 | 31 | 15 | 23 | 11 | 5 | 2 | 17 | 8 | 4 | 18 | 9 | 20 | 10 | 21 | 26 | 13 | 6 | 3 | 1 |
11011 | 16 | 24 | 12 | 22 | 11 | 21 | 10 | 5 | 18 | 9 | 4 | 2 | 17 | 8 | 20 | 26 | 29 | 30 | 31 | 15 | 23 | 27 | 13 | 6 | 19 | 25 | 28 | 14 | 7 | 3 | 1 |
11101 | 16 | 24 | 12 | 6 | 19 | 9 | 4 | 18 | 25 | 28 | 30 | 31 | 15 | 23 | 27 | 29 | 14 | 7 | 3 | 17 | 8 | 20 | 10 | 21 | 26 | 13 | 22 | 11 | 5 | 2 | 1 |
*In my code I refer to traps. Miss read the wiki :P
Wednesday, August 24, 2011
Overengineering
A while ago I used to be able to VNC from my phone to my desktop to remotely play, pause and change the volume of winamp. I didn't use it often but it was handy.
At some point, my phone just gave up on the VNC connection. I don't know whether it can't handle the screen size (poor iPhone 3g) or if there is an incompatibility in the VNC versions. I did try updating RealVNC and using a different client on my phone but no success.
I had toyed with Java Robot before and figured it would be worth a laugh.
So fired up eclipse and wrote a program that paused, played, turned up the volume and turned down the volume. Fumbled through the Robot javadoc and re-factored it a bit so it looked nice. Got it running fairly quickly.
Next problem, how to control it. I figured html would work for an interface. That means I need to set-up a http server to listen for requests. I was hoping to just fudge through it, read the socket until I got something that looked like a location and push back content.
I wasn't really in the mood to read through the RFC for http so I turned to the Internet.
It came back with NanoHTTPD
I wasn't happy with the way you were expected to subclass it to handle specific URLs so I re-factored it a bit before getting to work.
Put some html together to be served and then it basically fell together.
I love it when you start from the top and bottom of a problem and the middle is just a bit of fiddling around.
So here it is, my winamp remote:
Yup, not much but it made me a happy little coder.
Been wasting most of today re-factoring NanoHTTPD. One of it's selling points is that it is just a single file which was handy for this project, but annoys my sense of organisation. I've split it in 2, one for the pure Http stuff and the other for utilities like turning it into a file browser.
Got rid of the need to consistently spawn new threads, clean up it's stream handling a bit and brought it up to scratch in terms of Java 1.5.
Was wondering if anyone else was interested in this? Should I make the controls configurable so it can control other applications?
At some point, my phone just gave up on the VNC connection. I don't know whether it can't handle the screen size (poor iPhone 3g) or if there is an incompatibility in the VNC versions. I did try updating RealVNC and using a different client on my phone but no success.
I had toyed with Java Robot before and figured it would be worth a laugh.
So fired up eclipse and wrote a program that paused, played, turned up the volume and turned down the volume. Fumbled through the Robot javadoc and re-factored it a bit so it looked nice. Got it running fairly quickly.
Next problem, how to control it. I figured html would work for an interface. That means I need to set-up a http server to listen for requests. I was hoping to just fudge through it, read the socket until I got something that looked like a location and push back content.
I wasn't really in the mood to read through the RFC for http so I turned to the Internet.
It came back with NanoHTTPD
I wasn't happy with the way you were expected to subclass it to handle specific URLs so I re-factored it a bit before getting to work.
Put some html together to be served and then it basically fell together.
I love it when you start from the top and bottom of a problem and the middle is just a bit of fiddling around.
So here it is, my winamp remote:
Yup, not much but it made me a happy little coder.
Been wasting most of today re-factoring NanoHTTPD. One of it's selling points is that it is just a single file which was handy for this project, but annoys my sense of organisation. I've split it in 2, one for the pure Http stuff and the other for utilities like turning it into a file browser.
Got rid of the need to consistently spawn new threads, clean up it's stream handling a bit and brought it up to scratch in terms of Java 1.5.
Was wondering if anyone else was interested in this? Should I make the controls configurable so it can control other applications?
Subscribe to:
Posts (Atom)