Random Number Generator Issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Random Number Generator Issue

  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Random Number Generator Issue

    This is my specification for my Random Number Generator.

    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:
    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!!

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: Random Number Generator Issue

    Not sure how it effects the outcome, but after the first time through (first 256), you are setting both start and trail to 0. After this point lag will always be 0. Shouldn't you be setting start = 13 at that point to maintain your lag of 13?

  3. #3
    Join Date
    Jul 2012
    Posts
    90

    Re: Random Number Generator Issue

    After doing some experimenting...

    If you move

    val_s = lfg[start];
    val_t = lfg[trail];

    inside the for loop and set start = 13 if start or trail = 255 you get what looks like random numbers in all but the first 13 elements of lfg. Those first 13 elements are never touched (I assume since I'm not allowing start to ever be less than 13). If you don't do this it appears that after the first iteration of the for loop spin is always = 1.
    Last edited by CGKevin; September 14th, 2012 at 03:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center