9 Replies - 1103 Views - Last Post: 17 September 2012 - 06:13 AM Rate Topic: -----

#1 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Random Number Generator Issue

Posted 10 September 2012 - 07:34 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Generator Issue

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Random Number Generator Issue

Posted 10 September 2012 - 09:40 PM

You've incorrectly interpreted:

Quote

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

to
start++;
trail++;
if (start == 255 | trail == 255)
{
    start = 0;
    trail = 0;
}



I think that the first part is use of the incorrect OR operator to translate "when either of these".

The second part is the resetting both start and trail to zero when either on reaches 255. Since English is an ambiguous language at times. I think that what the algorithm was trying to say was that "when start reaches 255, make it 0; and when trail reaches 244, make it 0". This is the only thing that makes sense, because otherwise if you set both start and trail to zero at the same time, then you lose all the benefits of having the lag. And if you don't have the lag, why is the algorithm called "Lagged Fibonacci Generator"?

The last part, I'm uncertain with. I think that start or trail needs to be reset to zero only when it reaches 256, not 255. Otherwise one entire slot in the lfg array will remain unused. If it was meant to remain unused, why not make an array of 255 elements instead of the 256 currently prescribed?

Anyway assuming all those 3 parts are correct, then lines 47-54 can be rewritten as two lines with each line using a modulus operator. What is strange, though, is that step 2 explicitly said to use modulus operator, it could have just as easily said the same thing with regards to step 6.

This post has been edited by Skydiver: 10 September 2012 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Random Number Generator Issue

Posted 10 September 2012 - 10:01 PM

Hi,

I made the following changes to the code
if (start == 255/* | trail == 255*/)
                {
                    start = 0;
                    //trail = 0;

                }
                else if (trail == 255)
                {
                    trail = 0;
                }

but the output is still the same!?
Freaking 2197 every time
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Random Number Generator Issue

Posted 11 September 2012 - 12:48 AM

*sigh*

Quote

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


You implemented this inside the Next() method instead of inside the the constructor.

This random number generator is supposed to be fast, and now you've made it slower, but that isn't the real source of the problem.

The real source of the problem is that by having the for loop inside Next(), the correct values for val_s and val_t are not picked up for the succeeding iterations of the for loop. e.g. lines 35-36 should be inside the for loop.

So the correct way to fix this is have the for loop in the constructor as the instruction say it should.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Random Number Generator Issue

Posted 11 September 2012 - 06:19 AM

Read the line carefully. It says

Quote

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

Incrementing them at 255 gets you 0, not "when they are equal to 255 make them zero".
So you could use:
start = (start + 1 ) % 256;
trail = (trail + 1) % 256;

which gives you the full range of 0 to 255.

It also says

Quote

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

and not "Every time you call Next do it 256 * lag times". You only do this once, to initialize the random number generator.

This post has been edited by Momerath: 11 September 2012 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Random Number Generator Issue

Posted 12 September 2012 - 06:37 AM

Is it Correct?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment_1
{
    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;
        //private uint[] RN;
        
        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
            for (int i = 0; i < (256 * lag); i++)
            {
                Next();
            }
        }

        public uint Next()
        {
            Console.WriteLine(start);
            val_s = lfg[start];
            val_t = lfg[trail];

                spin = (int)(val_t % 32);


                val_s = (val_s << spin) | (val_s >> (32 - spin));
                val_s = val_s ^ val_t;
                lfg[start] = val_s;


                start = (start + 1) % 256;
                trail = (trail + 1) % 256;
                if (start == 255)
                {
                    start = 0;
                }
                if (trail == 255)
                {
                    trail = 0;
                }
                
            
            Console.WriteLine("{0}", val_s);
            return val_s;
        }



    }
}


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Random Number Generator Issue

Posted 12 September 2012 - 09:15 AM

You don't need lines 51-58. Lines 49 and 50 already have that effect as I noted back in post #2.

Do you even use the lag member variable?

Why are val_s, val_t, and spin member variables?
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Random Number Generator Issue

Posted 12 September 2012 - 11:57 AM

Not only don't you need lines 51-58, they actively break the implementation.

This post has been edited by Momerath: 12 September 2012 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 ashishnehete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-September 12

Re: Random Number Generator Issue

Posted 17 September 2012 - 03:45 AM

Thanks Guys for solving my issue!!
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: Random Number Generator Issue

Posted 17 September 2012 - 06:13 AM

View Postashishnehete, on 12 September 2012 - 02:37 PM, said:

Is it Correct?


Does it meet the requirements? Run / debug it find out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1