2

Possible Duplicate:
Random number generator not working the way I had planned (C#)
Why does it appear that my random number generator isn't random in C#?

I have a problem with random values

        int weight = 0;
        Random random = new Random();
        for (int i = 0; i < entriesCount; i++)
        {
            weight = random.Next(10);
        this.weights[i] = weight;
        }

This code is in constructor of my object. I create 3 diffrent objects

Object object1 = new Object(2);
Object object2 = new Object(2);
Object object3 = new Object(2);

For every object I get same random values for example: 4, 5 | 4, 5 | 4, 5

Every time I get same values in same sequence. I don`t get why> Please help

Best regards, Dawid

Community
  • 1
  • 1
Fixus
  • 4,631
  • 10
  • 38
  • 67
  • Here is some good reading on the subject: http://csharpindepth.com/Articles/Chapter12/Random.aspx – Oded Dec 08 '11 at 18:53
  • See the explanation here: [Creating a single Random object rather than multiple ones](http://msdn.microsoft.com/en-us/library/system.random.aspx) – Smash Dec 08 '11 at 18:45

4 Answers4

3

The problem is that you're creating a new Random each time. When you create an instance of the Random class, it uses the current time as a seed. If you do this multiple times very quickly, you get the same seed value, so the different Random instances output the same results.

In order to work around this, you need to either make sure your random is seeded uniquely each time, or share and use a single Random instance. The easiest option is to just make the Random instance static:

class YourClass
{
    private static Random randomGenerator = new Random();

    public YourClass(int entriesCount)
    {
       int weight = 0;
       for (int i = 0; i < entriesCount; i++)
       {
           weight = randomGenerator.Next(10);
           this.weights[i] = weight;
       }
    }
    // .. rest of your class

This will cause the class to always reuse the same Random instance, so you'll get different values each time.

Note that if you're going to be using this in a multithreaded scenario, you'll also have to synchronize access to the random instance, or come up with a different approach (such as saving a seed value, and using something like Interlocked.Increment to increment it and seed a new random from each instance, etc).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

Random is a pseudorandom number generator, which means the sequence of outputs is the same for any given seed. If you pass a seed to the constructor, you get a different sequence.

TJD
  • 11,800
  • 1
  • 26
  • 34
0

As far as I am aware, a random is seeded by the system time unless you specify otherwise. It generates numbers based on this number. As you create them almost exactly at the same time, they have the same seed and will almost always return the same number and sequence.

Any easy fix would be to create a static random all instances share, and just call .Next() on that static object.

Edwin
  • 13
  • 1
0

From the MSDN documentation:

"using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers."

competent_tech
  • 44,465
  • 11
  • 90
  • 113