Random Number Generator

The random number generator i want to create is called a Lagged Fibonacci Generator. It is initialized as follows.

1. Start with an array of 256 unsigned int’s (i.e lfg[]).

2. Starting at index location 0, fill the array with the numbers 0, 1, 8, 27, 64, 125, etc. That is, with the cube of the index location.

3. Assign to index location 0 the encryption key value supplied in the command line.

4. Have two fields called start and trail. start is set to 13 and trail is set to 0.

The difference between the two values (that is 13) is called the lag,

There is one more step to be completed before the random number generator is ready for use.

Before we can do this though, we need to know out how to generate the random numbers.

You will need to create a public method called Next, which returns an unsigned int and

performs the following operations.

1. Get the values in the array at location start and trail. Call them val_s and val_t

respectively.

2. Get val_t modulus 32. 32 is the number of bits in an unsigned int and will result in a

number in the range 0 to 31. Call this value spin.

3. Take the left most spin bits of val_s and move them to the right most position.

For example, assuming spin = 12 and val_s = 0x13ac8d08 then applying this spin

operation would result in val_s = 0xc8d0813a.

4. Make val_s = vals_s xor val_t.

5. Set the array at location start to val_s.

6. Increment start and trail by 1. When either of these get to 255 incrementing them

will make them equal to 0.

7. Return val_s as the randomly generated number.

As the final step in initializing the random number generator call Next a total of (256 * lag)

times.

Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; class RandonNoGen { private uint[] lfg; //Unsigned array of 256 size private const int lfgSize = 256; private uint start, trail,lag,val_s,val_t; private int spin; public RandonNoGen(uint key) { lfg = new uint[lfgSize]; start = 13;//Lag trail = 0;//Lag lag = start - trail; for (uint i = 0; i < lfg.Length ; i++) { lfg[i] = i * i * i; } lfg[0] = key;//Assign to index location 0 the encryption key //Trial Next(/*start, trail, lfg*/); } public uint Next(/*uint start, uint trail, uint[] lfg */) { Console.WriteLine(start); val_s = lfg[start]; val_t = lfg[trail]; for (int i = 0; i < (256 * lag) ; i++) { spin = (int)(val_t % 32); val_s = (val_s << spin) | (val_s >> (32 - spin)); // This is what i have used to rotate the bits val_s = val_s ^ val_t; lfg[start] = val_s; start++; trail++; if (start == 255 | trail == 255) { start = 0; trail = 0; } } Console.WriteLine("{0}", val_s); return val_s; } }

The problem is when i see the lfg array (Key=1234) after running the whole code the number 2197 keeps coming up in the array again and again and is also returned as val_s by the next().

which totally makes no sense to me.

I have been looking at this piece of code for last 2 sleepless nights and cant figure out what is wrong with it.

Help Guys!

Thanks!!