简体   繁体   中英

Adding Seed to C# random number always yields same result

whenever I have two random numbers being generated like so :

 Random rand1 = new Random();
 Random rand2 = new Random(23);

the second random number's value never changes

while rand1 varies on each load, rand2 always presents the value 396. Am I declaring the seed wrongly?

The second random value never change entirely by design. Moreover, if you store the first 100 or 1000 random numbers after seeding your generator to a fixed number, you would get the same sequence every time you run the program.

Seeding a pseudorandom number generator is designed specifically to let you produce a repeatable sequence of random numbers. This is very useful for testing your code that needs to use random numbers, but you want repeatable behavior for testing purposes.

In situations when you do not need to produce the same pseudorandom sequence again and again, you seed your random number generator to some changing number, for example, the current system time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM