0

Possible Duplicate:
Random number generator only generating one random number

Im trying to create a loop to create and output 5 random numbers in a listbox. Basically it outputs the same random number 5 times instead of 5 different ones. When I use a break point and go through the code it does actually generate the 5 numbers. So why does it only output the first answer? Thank you. (This is not the entirety of the project, but I need to get this working first).

    public string Numbertext1;
    public string Numbertext2;       
    public int GeneratedNumbers;
    public int Average = 0;
    public int TotalSum = 0;
    public int TotalCalcs = 0;
    public int Counter = 0;

    private void btnRandomise_Click(object sender, EventArgs e)
    {
        Numbertext1 = txtNum1.Text;
        int Number1;
        int.TryParse(Numbertext1, out Number1);
        Numbertext2 = txtNum2.Text;
        int Number2;
        int.TryParse(Numbertext2, out Number2);

        do
        {

            Random num = new Random();
            int number = num.Next(Number1, Number2);
            lbNumbers.Items.Add(Convert.ToString(number));
            Counter++;
        }
        while (Counter < 5);
        {
            TotalCalcs++;
            Counter = 0;
        }


    }
}
}
Community
  • 1
  • 1
Simon Roberts
  • 21
  • 1
  • 5
  • 1
    This is a *very* common question (see [C# FAQ](http://stackoverflow.com/tags/c%23/info)). Please ensure you take the time to see if your question hasn't been asked before. – Kirk Woll Oct 09 '12 at 00:54
  • 1
    I'm going to have to question your use of brackets after the `while (Counter < 5)` line. – Ichabod Clay Oct 09 '12 at 01:01
  • @IchabodClay It's *technically* legal syntax ;-) (There are many *additional* problems/upcoming-issues such as useless member variables.) –  Oct 09 '12 at 01:10

4 Answers4

6

You need to initialize your num variable at the global level. It's using the same seed over and over again.

Put this : Random num = new Random(); at the top where you are initializing everything else. Then remove it from inside your method.

davehale23
  • 4,374
  • 2
  • 27
  • 40
3

It's because you're creating a new Random instance within a tight loop, so the seed number will be the same. The Random class is not truly random (in the mathematical sense), so you should change the seed or use one instance of it. Move Random num = new Random(); to the top with the other variables.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
2

Because you didn't adequately seed the random number generator.

The generator has an algorithmn it follows, if you just create it without seeding it then your numbers will be the same each time. To quote from MSDN:

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.

To fix this use the other constructor which allows you to specify a seed - there is a good example of this on MSDN.

slugster
  • 49,403
  • 14
  • 95
  • 145
0

The Random class instantiation is time-dependent. With a very quick loop, you're creating the same object every time and thus you're getting the same value over and over. You need to move the instantiation outside the loop so you get new numbers when you call Next() .

This is also the reason why it "works" when you use a break point to check the values. The Random object you created will have different reference times and thus would be different.

rikitikitik
  • 2,414
  • 2
  • 26
  • 37