Duplicar posible:
Why does this Random Number Generator not random?¿Por qué no es aleatorio() aleatorio?
que tienen este programa de prueba:
static void Main(string[] args)
{
var randomNumbers = new Dictionary<int, int>();
foreach (var s in Enumerable.Range(1, 500))
{
var rand = Rand5();
if (!randomNumbers.ContainsKey(rand))
randomNumbers.Add(rand, 1);
else
randomNumbers[rand] += 1;
}
randomNumbers
.ToList()
.ForEach(x => Console.WriteLine("{0}: {1}", x.Key, x.Value));
Console.ReadLine();
}
static int Rand5()
{
System.Threading.Thread.Sleep(1);
return new Random().Next(1, 6);
}
Si comento hacia fuera System.Threading.Thread.Sleep(1);
, consigo
5: 500
Pero si elimino esa línea, obtengo números aleatorios.
2: 87
4: 94
1: 116
5: 108
3: 95
¿Por qué es importante la línea de código? ¡Gracias!
Supongo que es inútil marcar tales preguntas como engañados, ya que probablemente alrededor de un tercio de todas las * preguntas etiquetadas al azar son el mismo problema. – Joey