Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generator always picks the same value when run inside a loop

The problem with the code is, when I try to generate a number, if the spin is equal 1 it generates values inside range (1,2,3) if if try to use the loop to sum random values inside the same range the random number gerated is always the same while in loop,

example, if I run the loop with: spind3 = 4 the values go from 4, 8, 12 spind3 = 5 the values go from 5, 10, 15

That means the first time the RandomNumber generates a value inside loop, it never change until the loop completes.

if (toggled3.Checked)
   {
    if (spind3.Value != 1)
        {           
         for (int i = 1; i <= spind3.Value; i++)
              {
               diceCalc[1] += RandomNumber(1, 4);
              }
        }
     else
     diceCalc[1] = RandomNumber(1, 4);
     }
like image 740
Primordium Avatar asked Mar 22 '11 21:03

Primordium


People also ask

Can you generate the same random numbers everytime?

random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function.

Why is Rand generating the same number?

The RAND function in stand-alone applications generates the same numbers each time you run your application because the uniform random number generator that RAND uses is initialized to same state when the application is loaded.

Are random number generators accurate?

Random number generators are typically software, pseudo random number generators. Their outputs are not truly random numbers. Instead they rely on algorithms to mimic the selection of a value to approximate true randomness.

Can a random number generator be rigged?

With some random number generators, it's possible to select the seed carefully to manipulate the output. Sometimes this is easy to do. Sometimes it's hard but doable. Sometimes it's theoretically possible but practically impossible.


3 Answers

You are probably creating a new Random object inside RandomNumber method. The default constructor for Random uses the system time as a seed. If you create multiple Random objects in a tight loop the time probably won't have changed between each call so they will all be initialized with the same seed.

To fix your code you should only create one Random object and reuse it.


From the documentation:

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.

like image 157
Mark Byers Avatar answered Oct 31 '22 15:10

Mark Byers


The problem is that you are creating random generators too close in time. The random generator constructor uses the current time to seed the generator, and when you create them too close in time they will all be seeded using the same time.

Create one random generator and use in the loop:

Random rnd = new Random();
for (int i = 1; i <= spind3.Value; i++) {
  diceCalc[1] += rnd.Next(1, 4);
}
like image 42
Guffa Avatar answered Oct 31 '22 15:10

Guffa


You need to initialize your Random object, then call Next() inside your loop.

i.e.

if (toggled3.Checked)
{
  // initialize your total and the random number generator
  int diceTotal = 0;
  Random rand = new Random();

  for (int i = 0; i < spind3.Value; i++)
  {
    // add the next random number between 1 and 3
    diceTotal += rand.Next(1, 4); 
  }
}
like image 37
Winger Avatar answered Oct 31 '22 17:10

Winger