C # Random Random duplicate values solution

 

Using the code shown in FIG above, there will be a case, obviously random, the value is the same, such a random probability is too small, it is estimated that the code in question.


Thus the search, find the cause of this problem is in C # Random is to generate random numbers based on the time, and generates fast. Go to the Random definitions can be found after his comments is the time of seed values, as shown below:

 

Online, there are many ways to solve, mainly two: First, use Thread.Sleep () to set the required time value, the second is to find ways to make new Random (int Seed) parameters in the randomized, refer to https: // www .cnblogs.com / duanjt / the p-/ 7064100.html .

But suddenly a flash, a new discovery: that the var random = new Random (); This code out of loops can be solved, as shown below:

Let us look at the results:

perfect

Guess you like

Origin www.cnblogs.com/EmptyBlog/p/10990960.html