A random number is defined as: All the numbers produced have no relation.
Random numbers are used in many places in real-world applications, such as the need to generate unique order numbers.
There are three ways to get a random number in C #:
A. Random class
The default parameterless constructor of the random class can be seeded based on the current system clock, making a series of algorithms to obtain pseudo-random numbers within the required range.
12 |
Random rd = new Random(); int i = rd.Next(); |
This random number can achieve some low-demand targets, but if the random class takes the system clock seed close or even exactly the same in high concurrency, there is a good chance of repetition, where the loop is used to illustrate
12345 |
for ( int i = 0; i < 10; i++) { Random rd = new Random(); //无参即为使用系统时钟为种子 Console.WriteLine(rd.Next().ToString()); } |
This example outputs 10 identical "random numbers".
Highlight the problem: because random random number algorithm is fixed, so the number calculated according to the same seed must be the same. And with the speed of the modern computer, the cycle is almost instantaneous, the seed is consistent, so there will be 10 cycles to output the same random number of cases.
Two. Guid class
System.Guid
GUID (Globally unique Identifier) Global Unique identifier
GUIDs are calculated using a number of native-ready numbers, such as the hardware ID code, the current time, and so on. The computed 128-bit integer (16 bytes) can be close to the unique output.
1 |
Console.WriteLine(Guid.NewGuid().ToString()); |
The result is a 16-digit number for the XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX structure. Of course, this format can also be changed.
Three. RNGCryptoServiceProvider class
System.Security.Cryptography.RNGCryptoServiceProvider
RNGCryptoServiceProvider using the implementation provided by the cryptographic service provider (CSP) to implement the cryptographic random number generator (RNG)
1234 |
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 the calculated random numbers are different, even if they are placed in a loop.
1234567 |
for
(
int i = 0; i < 10; i++)
{
RNGCryptoServiceProvider csp =
new
RNGCryptoServiceProvider();
byte
[] byteCsp =
new
byte
[10];
csp.GetBytes(byteCsp);
Console.WriteLine(BitConverter.ToString(byteCsp));
}
|
1 |
但是RNGCryptoServiceProvider的计算较为繁琐,在循环中使用会消耗造成大量的系统资源开销,使用时需注意. |
Membership.generatepassword ()
Membership is a quick and easy class for role rights management, and has stumbled upon an interesting approach that has not been studied.
1234567891011121314 |
public
static
string
GeneratePassword(
int length,
int
numberOfNonAlphanumericCharacters);
//
// 摘要:
// 生成指定长度的随机密码。
//
// 参数:
// numberOfNonAlphanumericCharacters:
// 生成的密码中的标点字符数。
//
// length:
// 生成的密码的字符数。长度必须介于 1 和 128 个字符之间。
//
// 返回结果:
// 指定长度的随机密码。
|
Cases:
1234 |
for ( int i = 0; i < 10; i++) { Response.Write(Membership.GeneratePassword(20, 1) + "<br>" ); } |
Result is
c!&^hotnv3! Zhkk9babu
Azlger) jj-uw8q*14yz*
i3qnb]zxu16ht!kkz! q*
9U:MAQ&C1X) ^[email protected]**
OL (%4jvfbp&t5*hpl4l-
[Email protected] $CnhW &d+|xof:qik
a/! Di&l*ty$qamh0gyzy
z^wu6{1bmq7d^+wu]>f$
1ogijs3&09fw0f9.| AXA
8f+gy+l{o6x{sfugme*%
There are three ways to get random numbers in C #