This question already has an answer here:
I have a "randomize" method, and the results more or less check out, but when I print out the random values, they seem to be grouped, despite using a new Random object each time.
int zeros = 0;
int ones = 0;
int twos = 0;
int threes = 0;
int fours = 0;
for (int i = 0; i < 1000; i++)
{
var number = new Random().Next(5);
switch (number)
{
case 0:
zeros++;
break;
case 1:
ones++;
break;
case 2:
twos++;
break;
case 3:
threes++;
break;
case 4:
fours++;
break;
default:
break;
}
WriteLine(number);
}
WriteLine("0: " + zeros);
WriteLine("1: " + ones);
WriteLine("2: " + twos);
WriteLine("3: " + threes);
WriteLine("4: " + fours);
ReadLine();
Now what I find most curious about it:
1) If I leave out the "WriteLine(number);", it gives me 1000 times the same result.
2) When leaving in the "WriteLine(number);" line, it gives me grouped values, split correctly percentagewise. (e.g.: lots of 3's followed by lots of 2's)
3) I know that the easy fix is this:
...
var random = new Random();
for (int i = 0; i < 1000; i++)
{
var number = random.Next(5);
...
But I was curious as for why case 1) and 2) differ so much from 3)?
Aucun commentaire:
Enregistrer un commentaire