-2

Why does this work and produce different random numbers per the SIZE?

for (int index = 0; index < SIZE; index++)
{
    Random rand = new Random();
    numbersArray [index] = rand.Next(0, 100);
    MessageBox.Show(index.ToString());
}

And yet this produces the same number per the SIZE?

for (int index = 0; index < SIZE; index++)
{
    Random rand = new Random();
    numbersArray [index] = rand.Next(0, 100);
}

The only guess i have is that the Random object gets refreshed when the program pauses?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • When asking a question like this, it's useful to show us what your output is (or at least what it looks like). Your first output should look like `0, 1, 2, …` (in a message box!). You second one should be random, but who knows, you are creating new instances of System.Random in a very tight loop. For what it's worth, you can use `Debug.WriteLine` to output kinda-sorta console input from a Windows Forms app (it will show up in the Output pane) – Flydog57 Nov 26 '18 at 03:56
  • 1
    It is always helpful to consult the documentation before posting a question. This is issue is covered in the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.random?redirectedfrom=MSDN&view=netframework-4.7.2) – Ňɏssa Pøngjǣrdenlarp Nov 26 '18 at 04:48

1 Answers1

-2
    for (int index = 0; index < SIZE; index++)
    {
        Random rand = new Random();
        numbersArray [index] = rand.Next(0, 100);
        //index is not the actual random number!
        MessageBox.Show(index.ToString());
    }

In this code you are showing yourself the index number of the array, not the actual random number, maybe this is the reason why you don't get the expected result. ;)

Aaron H
  • 134
  • 10
  • Why are people downvoting my answer? I do not get what is wrong with it, could you please explain what I did wrong? Thank you! – Aaron H Nov 26 '18 at 04:49
  • I did not downvote you but if you read again what the OP is saying - that piece of code with the `MessageBox` is generating different random numbers compared to the one without the `MessageBox`. I think he is using the `MessageBox` as a way to pause the loop which would explain the behaviour. – Ilian Nov 26 '18 at 05:37
  • @IlianPinzon Oh, I see, I thought the MessageBox thing was to output the random number. But if the OP actually was asking why pausing the loop makes the numbers different, the "duplicate" mark actually would be wrong, wouldn't it? – Aaron H Nov 26 '18 at 05:52
  • I think it's correct. Pausing the loop will create a `Random` object with a different seed. Without the pause, all/most of the created `Random` objects will have the same seed. – Ilian Nov 26 '18 at 06:01
  • @IlianPinzon I don't quite understand why the creation of the new Random object is happening just because the program is paused? Could you explain it or send me a link about it? I'm not sure what exactly to search for. Thanks! – Aaron H Nov 26 '18 at 06:28
  • 2
    With or without the pause, the `Random` objects are created. The seed for the random number generator uses the computer's clock. If the loop is very fast, the same seed will be used (same time from the clock). Check the duplicate question and read the answers, the explanation is more detailed there. :) – Ilian Nov 26 '18 at 06:32
  • @Ilian Okay, got it! Thanks! – Aaron H Nov 26 '18 at 07:28