可能重複:
Why does it appear that my random number generator isn't random in C#?
Random number generator not working the way I had planned (C#)隨機化數字不起作用
你好,
我用這個函數(類)以隨機數字:
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
public void Init()
{
x = RandomNumber(0,500);
}
但是如果我再打多個對象做到這一點:
Obj[] obj = new Obj[64];
for(int i = 0 ; i < 64 ; i++)
{
obj[i] = new Obj();
}
...
for(int i = 0 ; i < 64 ; i++)
{
obj[i].Init();
}
然後每個對象都有完全相同的「x」值。
這裏有什麼問題?
這裏是一個更好的重複與線程安全的答案:http://stackoverflow.com/questions/767999/random-number-generator-not-working-the-way-i-had-planned-c – 2011-04-03 19:45:31