TheChilliPL TheChilliPL - 4 months ago 45
C# Question

C# - Random number with seed

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...



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?


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.