Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The method of generating random numbers


May 12, 2021 C#



There are several ways to generate random numbers in C#, the following three of which are most commonly used.

  1. Use the Random class
  2. Use the Guide class
  3. Use the RNGCryptoServiceProvider class

1, using the Random class

The default non-parameter constructor of the Random class can make a series of algorithms to arrive at pseudo-random numbers within the required range based on the current system clock as the seed

Random rd = new Random()
rd.next(1,10)(生成1~10之间的随机数,不包括10)

The above is an example of a random number within 10, which can achieve some lower-requirement targets, but if the system clock seed taken by the Random class is close to or even exactly the same in high-level conditions, there is a good chance of duplication, as shown in the loop here

  for(int i=0;i<10;i++)
            {
                Random rd = new Random();
                Console.WriteLine(rd.Next(10,100).ToString());
            }

This example will get 10 identical random numbers, the time to complete the cycle is very short, so the random number calculated according to the system time as a seed will be the same. So the Random loop is only available in low-requirement situations.

2, using guide class

System.Guid

Guid (Globally Unique Identifier) is the only identifier in the world

GUID calculations use a lot of numbers that are desirable on this machine, such as hardware ID code, current time, etc. T he calculated 128 - bit integer (16 bytes) can be close to the only output.

Console.WriteLine(Guid.NewGuid().ToString());

The calculation result is a 16-step number of xxxxxx-xxxx. O f course this format can also be changed. T here are four formats that are commonly used:

 var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
 var uuidN = Guid.NewGuid().ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09 
 var uuidD = Guid.NewGuid().ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
 var uuidB = Guid.NewGuid().ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}
 var uuidP = Guid.NewGuid().ToString("P"); //  (ade24d16-db0f-40af-8794-1e08e2040df3)
 var uuidX = Guid.NewGuid().ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}}

3, using RNGCryptoServiceProvider class

System.Security.Cryptography.RNGCryptoServiceProvider

RNGCryptoServiceProvider uses an implementation provided by the Encryption Service Provider (CSP) to implement the Encrypted Random Number Generator (RNG)

RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
byte[] byteCsp = new byte[10];
csp.GetBytes(byteCsp);
Console.WriteLine(BitConverter.ToString(byteCsp));

Because this class uses a more rigorous algorithm. S o even if you put it in the loop as follows, the calculated random numbers are different.

for (int i = 0; i < 10; i++)
{
    RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
    byte[] byteCsp = new byte[10];
    csp.GetBytes(byteCsp);
    Console.WriteLine(BitConverter.ToString(byteCsp));
}

However, RNGCryptoServiceProvider calculation is more cumbersome, in the cycle will consume a lot of system resources overhead, use should be noted.