C# Why does the generated random number repeat and generate non-repeated random numbers

Lionsure 2020-08-27 Original by the website

Sometimes the random numbers generated are not repeated, and sometimes the generated random numbers are all repeated random numbers. Why does this happen? Comparing the generation of repeated and non-repeated random numbers, it can be found that the generation of repeated random numbers often occurs in the loop, and the random number repetition rarely occurs in other situations. From here, it can be found that the short interval between two random number generations will generate repeated random numbers, and the loop is generally executed very fast(in milliseconds, the values are very small). This gives us a revelation, programs that do not usually generate repeated random numbers may generate repeated random numbers when the interval between the user's two visits is very small, resulting in program execution errors. This error is a potential error and is not easy to be found, so software errors are always incomplete.

After the above comparison and analysis, you must know a little bit about why the random numbers are repeated. Next, we will make a summary, and then use code examples to illustrate how to generate non-repeated random numbers.


1. The reason for C# random number duplication

The above example of generating random numbers in a loop shows that when the time interval between two generating random numbers is relatively short, repeated random numbers will be generated. Why? To figure out this problem, I also understand why repeated random numbers are generated. It turns out that when the interval between two random numbers generation is very short, C# does not rush to generate seeds, so the random numbers generated use one seed, so repeated random numbers appear.


2. C# generate non-repeated random numbers

Since the interval is too short to generate seeds in a hurry, we can generate a seed for it so that repeated random numbers will not be generated. The code is as follows:

using System.Security.Cryptography;

/// <summary>
       /// C# generate non-repeated random numbers
       /// </summary>
       /// <param name="min">Generate random number minimum</param>
       /// <param name="max">Generate maximum random number</param>
       /// <returns>Random number generated</returns>

       public static int GetRandom(int min, int max)
              Random ran = new Random(GetRandomSeed());
              return ran.Next(min, max);

/// <summary>
       /// Generate random number seed
       /// </summary>
       /// <returns>Seed</returns>

       private static int GetRandomSeed()
              byte[] bytes = new byte[4];
              RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();

              return BitConverter.ToInt32(bytes, 0);


for (int j = 0; j < 10; j++)
              Response.Write("<br />" + GetRandom(100, 1000));

Output result:



There is also a non-repetitive random number generation method, which is to use the thread's sleep() method to generate a random number every 15 milliseconds, and will not generate repeated random numbers. If the efficiency is not high, it can be used.