1. Random class
The default non-parametric constructor of the Random class uses the current system clock as the seed to generate pseudo-Random numbers within the required range using a series of algorithms.
12 |
Random rd = new Random(); int i = rd.Next(); |
This Random number can achieve low requirements. However, if the system clock seed obtained by the Random class is close to or even identical in high concurrency, there may be duplicates, here we use loops for example.
12345 |
for ( int i = 0; i < 10; i++) { Random rd = new Random(); // If no parameter is specified, the system clock is used as the seed. Console.WriteLine(rd.Next().ToString()); } |
In this example, 10 identical "random numbers" are output ".
Highlighted problem: Because Random's pseudo-Random number algorithm is fixed, the numbers calculated based on the same seed must be the same. with the speed of running modern computers, the cycle is almost completed in an instant, and the seeds are consistent, so the same random number will be output in 10 cycles.