Pages

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).

	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
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.
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

No comments:

Post a Comment