Random not really random...

Discussion in 'ASP .Net' started by Maziar Aflatoun, Aug 4, 2004.

  1. Hi everyone,

    I have the following code in my class method

    TheSeed = (int)DateTime.Now.Ticks;
    Random rndNum = new Random(TheSeed);
    RandNum = rndNum.Next(0, TotalRows);
    Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);

    My page gets called everytime and these are the values that I'm getting for
    TotalRows = 8

    RandNum:1 Low:0 High:8
    RandNum:0 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:1 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:0 Low:0 High:8
    RandNum:0 Low:0 High:8
    RandNum:1 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:1 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:1 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:3 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:2 Low:0 High:8
    RandNum:7 Low:0 High:8

    Does anyone know why C# Random function is generating more 1,0,2,3 much more
    frequently than 4,5,6,7,8?

    Thank you
    Maziar A.
     
    Maziar Aflatoun, Aug 4, 2004
    #1
    1. Advertising

  2. No computer can generate a truly random number on its own.
    In addtion, the Math.Random class is not as random as it could be.
    For better results, try using the
    System.Security.Cryptography.RandomNumberGenerator class

    Here's more info:
    http://msdn.microsoft.com/library/d...yptographyrandomnumbergeneratorclasstopic.asp

    http://www.dotnet247.com/247reference/msgs/31/158262.aspx

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net



    "Maziar Aflatoun" <> wrote in message
    news:QCbQc.12749$...
    > Hi everyone,
    >
    > I have the following code in my class method
    >
    > TheSeed = (int)DateTime.Now.Ticks;
    > Random rndNum = new Random(TheSeed);
    > RandNum = rndNum.Next(0, TotalRows);
    > Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" +

    TotalRows);
    >
    > My page gets called everytime and these are the values that I'm getting

    for
    > TotalRows = 8
    >
    > RandNum:1 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:7 Low:0 High:8
    >
    > Does anyone know why C# Random function is generating more 1,0,2,3 much

    more
    > frequently than 4,5,6,7,8?
    >
    > Thank you
    > Maziar A.
    >
    >
     
    Steve C. Orr [MVP, MCSD], Aug 4, 2004
    #2
    1. Advertising

  3. Random means random. Roll a dice six times, and see what the spread is.
    Chances are, at least one number will come up at least twice, and at least
    one other won't come up at all. Random values only spread out evenly when
    the number of statistics is very high.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Maziar Aflatoun" <> wrote in message
    news:QCbQc.12749$...
    > Hi everyone,
    >
    > I have the following code in my class method
    >
    > TheSeed = (int)DateTime.Now.Ticks;
    > Random rndNum = new Random(TheSeed);
    > RandNum = rndNum.Next(0, TotalRows);
    > Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" +

    TotalRows);
    >
    > My page gets called everytime and these are the values that I'm getting

    for
    > TotalRows = 8
    >
    > RandNum:1 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:0 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:1 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:3 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:2 Low:0 High:8
    > RandNum:7 Low:0 High:8
    >
    > Does anyone know why C# Random function is generating more 1,0,2,3 much

    more
    > frequently than 4,5,6,7,8?
    >
    > Thank you
    > Maziar A.
    >
    >
     
    Kevin Spencer, Aug 4, 2004
    #3
  4. Maziar Aflatoun

    Jack Peacock Guest

    "Maziar Aflatoun" <> wrote in message
    news:QCbQc.12749$...
    > Hi everyone,
    >
    > I have the following code in my class method
    >
    > TheSeed = (int)DateTime.Now.Ticks;
    > Random rndNum = new Random(TheSeed);

    The above should be done one time only. The following lines should be
    iterated.
    > RandNum = rndNum.Next(0, TotalRows);
    > Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" +

    TotalRows);
    >
    > Does anyone know why C# Random function is generating more 1,0,2,3 much

    more
    > frequently than 4,5,6,7,8?
    >

    For starters your random seed doesn't look to be very random. Are you
    creating/destroying the rndNum object for every number? If so then you
    aren't making use of a randomizing algorithm. Every number will be the
    first in the sequence based on the supplied seed. If you call it twice with
    the same seed (i.e. within one tick) you will get the same number. You
    might notice your numbers repeat often, a sure sign you are within one tick
    on two successive iterations (this would cause a failure on a runs test for
    randomness.) The distribution of random numbers over your range will be
    very poor (as you have seen) and a chi-square statistical test won't do too
    well either to test your set of numbers for a large number of iterations.
    Try generating the same sequence, but only calling the .Next method inside
    your loop and preserve the rndNum object without resetting the seed each
    time.
    Jack Peacock
     
    Jack Peacock, Aug 5, 2004
    #4
  5. Thanks for your feedback. This is a banner distribution application. So
    it's called everytime the page is called. How can I call the .Next method
    inside my loop more than once?

    Thanks
    Maz.


    "Jack Peacock" <> wrote in message
    news:%...
    > "Maziar Aflatoun" <> wrote in message
    > news:QCbQc.12749$...
    >> Hi everyone,
    >>
    >> I have the following code in my class method
    >>
    >> TheSeed = (int)DateTime.Now.Ticks;
    >> Random rndNum = new Random(TheSeed);

    > The above should be done one time only. The following lines should be
    > iterated.
    >> RandNum = rndNum.Next(0, TotalRows);
    >> Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" +

    > TotalRows);
    >>
    >> Does anyone know why C# Random function is generating more 1,0,2,3 much

    > more
    >> frequently than 4,5,6,7,8?
    >>

    > For starters your random seed doesn't look to be very random. Are you
    > creating/destroying the rndNum object for every number? If so then you
    > aren't making use of a randomizing algorithm. Every number will be the
    > first in the sequence based on the supplied seed. If you call it twice
    > with
    > the same seed (i.e. within one tick) you will get the same number. You
    > might notice your numbers repeat often, a sure sign you are within one
    > tick
    > on two successive iterations (this would cause a failure on a runs test
    > for
    > randomness.) The distribution of random numbers over your range will be
    > very poor (as you have seen) and a chi-square statistical test won't do
    > too
    > well either to test your set of numbers for a large number of iterations.
    > Try generating the same sequence, but only calling the .Next method inside
    > your loop and preserve the rndNum object without resetting the seed each
    > time.
    > Jack Peacock
    >
    >
     
    Maziar Aflatoun, Aug 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amir
    Replies:
    3
    Views:
    609
  2. Kevin
    Replies:
    19
    Views:
    1,146
    Tris Orendorff
    Feb 13, 2006
  3. nc
    Replies:
    1
    Views:
    511
    nice.guy.nige
    Feb 3, 2005
  4. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,218
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page