1

When I executes the following code I'm getting the same color selected always.

static void Main(string[] args)
        {
            string[] Colors = new string[10] { "Red", "Yellow", "Green", "Blue", "Purple", "White", "violet", "orange", "indigo", "blue" };
            for (int i = 0; i < 13; i++)
            {
                Random rnd = new Random();
                int code = rnd.Next(0, 9);
                string Color = Colors[code];
                Console.WriteLine(Color);
            }
            Console.ReadLine();
        }

But if ` Random rnd = new Random();' is created outside the loop, then the result is unique. If the loop executes in faster rate then the output will be the same. Suppose I do some Database insert operation in the for loop the result will be a different one (a random one)(step by step execution using break points will also result random selection).

Is it really impossible to supply different seeds for such a small duration?

Subin Jacob
  • 4,692
  • 10
  • 37
  • 69
  • 2
    Your exact problem gets addressed in [the documentation for `Random`](http://msdn.microsoft.com/en-us/library/h343ddh9.aspx). `Random` instances are seeded with system time by default. Your loop is fast enough for them to have the same seed. Why are you generating a new RNG on each iteration anyway? – Carsten Mar 25 '13 at 09:54
  • @Arran I saw it only now! Please close it before I'm attacked. I can't delete it- already answered – Subin Jacob Mar 25 '13 at 09:55
  • @Carsten I just tried! But got confused seeing it – Subin Jacob Mar 25 '13 at 09:56

5 Answers5

5

Random uses current time as a seed. When you create it in loop, it happens so fast that time stays the same for every creation. So seed is the same, and values, generated by Random will be the same too.

alex
  • 12,464
  • 3
  • 46
  • 67
2

Try make random object a static member:

private static Random rnd = new Random();

This prevents building several random objects with a same seed (current time), and prevents producing a same sequence of numbers.

Your loop initializes a new instance of Random with the same seed (current time) upon each iteration. Each instance contains a sequence of various random numbers. The code uses the first number from the sequence, and after the iteration is finished the random object is thrown away, and a new Random object is instantiated. Since the code has been run quite fast, the next random object is created at the same time as the previous one, therefore it has the same seed as that one. The new object contains a sequence of various numbers, but the sequence is the same as the previous one (i.e., they have a same first number, second number, and so forth). Again the code uses the first number from the very same sequence, which results in repetitive numbers.

If you make the Random object a static member, the random sequence is created once, and the code will use the next number (not always the first number) of that sequence, hence you will iterate through the sequence of various random numbers.

If you don't want to make the random object a static member, try to feed its constructor a unique seed. You can make use of your loop variable for this purpose.

Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
2

If you don't provide a seed, Random will use Environment.TickCount for a seed. In a short loop like this, it's entirely possible that the the whole loop is executed in one tick. So the seed is the same every time, hence your "random" number is, too.

Just use the same random object for the entire loop.

Rik
  • 28,507
  • 14
  • 48
  • 67
2

Random is not random in computer programming ;) You can make it "more" random by including a seed or by having a static object containing the random :)

Ken de Jong
  • 219
  • 1
  • 11
0

you need to keep the same random object for the reason below:

Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated.

http://msdn.microsoft.com/en-gb/library/system.random.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70