一遍又一遍地生成相同的隨機數序列
使用相同的種子建立 Random
例項時,將生成相同的數字。
int seed = 5;
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Random instance " + i);
Random rnd = new Random(seed);
for (int j = 0; j < 5; j++)
{
Console.Write(rnd.Next());
Console.Write(" ");
}
Console.WriteLine();
}
輸出:
Random instance 0
726643700 610783965 564707973 1342984399 995276750
Random instance 1
726643700 610783965 564707973 1342984399 995276750