Three Methods of Obtaining and Generating Random Numbers in C

  • 2021-07-09 09:03:56
  • OfStack

Random numbers are defined as: all the numbers produced have nothing to do with each other.

Random numbers are used in many places in practical applications, such as the need to generate an order number with only 1.

There are three ways to get random numbers in C #:

1. Random class

The default parametric constructor of Random class can perform a series of algorithms to obtain pseudo-random numbers within the required range according to the current system clock as the seed.


Random rd = new Random();
int i = rd.Next();

This kind of random number can achieve the goal of 1, but if the system clock seed taken by Random is close to or even completely 1 in the case of high concurrency, it is very likely to be repeated. Here, a loop is used as an example

for (int i = 0; i < 10; i++)
{
    Random rd = new Random();    // No parameter means using the system clock as the seed
    Console.WriteLine(rd.Next().ToString());
}

This example will output 10 identical "random numbers".

The problem highlighted: Because the algorithm of Random for pseudo-random numbers is fixed, the number calculated according to the same seed must be 1. At the running speed of modern computers, the cycle is almost completed in an instant, and the seed is 1, so there will be 10 cycles to output the same random number.

2. Guid class

System.Guid

GUID (Globally Unique Identifier) Global Unique Identifier

The calculation of GUID uses many locally available numbers, such as hardware ID code, current time, etc. The calculated 128-bit integer (16 bytes) can approach the only one output.


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

The result is a hexadecimal number with the structure of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

3. RNGCryptoServiceProvider class

System.Security.Cryptography.RNGCryptoServiceProvider

RNGCryptoServiceProvider implements the cryptographic random number generator (RNG) using the implementation provided by the cryptographic service provider (CSP)


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, even if it is put in a 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, the calculation of RNGCryptoServiceProvider is cumbersome, and it will consume a lot of system resources when used in the loop, so attention should be paid when using it. '

Membership.GeneratePassword()

Membership is a quick and convenient role management class, accidentally found a very interesting method, did not study how to achieve


public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters);
//
// Summary :
//     Generates a random password of the specified length.
//
// Parameter :
//   numberOfNonAlphanumericCharacters:
//     The number of punctuation characters in the generated password.
//
//   length:
//     The number of characters of the password generated. The length must be between 1 And 128 Between three characters.
//
// Return results :
//     A random password of the specified length.

Example:


for (int i = 0; i < 10; i++)
{
    Response.Write(Membership.GeneratePassword(20, 1) + "<br>");
}

The result is


C!&^HoTNv3!ZHkK9BAbu azLgER)JJ-UW8q*14yz* I3qnb]Zxu16ht!kKZ!Q* 9U:MAQ&c1x)^aed@xe** oL(%4JvfbP&t5*Hpl4l- 6@zj$CnhW&D+|xOf:qIk A/!Di&l*tY$QaMH0gyzY z^wu6{1BMq7D^+WU]>f$ 1OgIJS3&09fw0F9.|aXA 8F+Gy+L{O6x{SfugME*%

I wonder if it just meets your requirements.


Related articles: