This article mainly introduces the three methods of obtaining and generating random numbers in C #, this article explains the random class generation method, the Guid class generation method and the RNGCryptoServiceProvider class generation method, the need friend can refer to the following
A random number is defined as: all numbers produced have nothing to do with it.
In practical applications, random numbers are used in many places, such as the need to generate a unique order number.
There are three ways to get random numbers in C #:
I. Random class
Random class The default parameterless constructor can be based on the current system clock as a seed, a series of algorithms to obtain the required range of pseudo-random numbers.
The code is as follows:
Random rd = new Random ();
int i = Rd. Next ();
This random number can achieve some of the lower requirements of the target, but if in high concurrency, the random class to fetch the system clock seed close to even exactly the same, it is likely to repeat, here with a loop to illustrate
The code is as follows:
for (int i = 0; i < i++)
{
Random rd = new Random (); No parameter is used to seed the system clock
Console.WriteLine (Rd. Next (). ToString ());
}
This example will output 10 identical "random numbers".
Highlights the problem: because the random algorithm for pseudorandom numbers is fixed, the number calculated from the same seed must be the same. With the speed of the modern computer, the cycle is almost instantaneous and the seed is consistent, so there will be 10 cycles of output of the same random number.
Two. Guid class
System.Guid
GUID (globally unique Identifier) globally unique identifier
The GUID is calculated using a number of digits that are acceptable to the computer, such as the hardware ID code, the current time, and so on. The calculated 128-bit integer (16 bytes) can be approximated to a unique output.
The code is as follows:
Console.WriteLine (Guid.NewGuid (). ToString ());
The result is a 16-digit number of the XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX structure. Of course, this format can also be changed.
Three. RNGCryptoServiceProvider class
System.Security.Cryptography.RNGCryptoServiceProvider
RNGCryptoServiceProvider implements the cryptographic random number generator (RNG) using the implementation provided by the cryptographic service provider (CSP)
The code is as follows:
RNGCryptoServiceProvider CSP = new RNGCryptoServiceProvider ();
byte[] BYTECSP = new BYTE[10];
Csp. GetBytes (BYTECSP);
Console.WriteLine (Bitconverter.tostring (BYTECSP));
This class uses a more rigorous algorithm. So even if the following is placed in the loop, the calculated random number is different.
The code is as follows:
for (int i = 0; i < i++)
{
RNGCryptoServiceProvider CSP = new RNGCryptoServiceProvider ();
byte[] BYTECSP = new BYTE[10];
Csp. GetBytes (BYTECSP);
Console.WriteLine (Bitconverter.tostring (BYTECSP));
}
However, the calculation of the RNGCryptoServiceProvider is cumbersome, the use of the cycle will consume a large amount of system resource overhead, need to pay attention.
Membership.generatepassword ()
Membership is a convenient and quick role rights Management class, found a very interesting way, did not study how to achieve
The code is as follows:
public static string Generatepassword (int length, int numberofnonalphanumericcharacters);
//
Summary:
Generates a random password of the specified length.
//
Parameters:
Numberofnonalphanumericcharacters:
The number of punctuation characters in the generated password.
//
Length
The number of characters for the generated password. The length must be between 1 and 128 characters.
//
return Result:
A random password that specifies the length.
Cases:
The code is as follows:
for (int i = 0; i < i++)
{
Response.Write (Membership.generatepassword (20, 1) + "
");
}
Result is
The code is as follows:
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 don't know if it just fits your requirements.