C# - Random number with seed

半世苍凉 提交于 2019-12-23 15:17:13

问题


I have this code:

var rand = new Random(0);
for(int i = 0; i < 100; i++)
{
  Console.WriteLine(rand.Next(0, 100));
}

And program should give me 100 times the same number (because seed is the same), but it gives different numbers...
Why?

Edit:
When I will do

for(int i = 0; i < 100; i++)
{
  Console.WriteLine(new Random(0).Next);
}

That returns the same number every time. That means, seed is changing? If yes, how? Is it increasing?


回答1:


It should not give you 100 same numbers but it should give you exactly the same 100 numbers each time you restart the app.

Seed is used to make random predictable. Imagine multiplayer game where you want something to be random. But you want to make sure that this random behaves the same for each player/client. And seed is the way to go here.



来源:https://stackoverflow.com/questions/39538479/c-sharp-random-number-with-seed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!