There are three ways to get random numbers in C #

Source: Internet
Author: User

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 = newRandom();inti = 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 = newRandom();  //无参即为使用系统时钟为种子    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 = newRNGCryptoServiceProvider();byte[] byteCsp = newbyte[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 = newRNGCryptoServiceProvider();    byte[] byteCsp = newbyte[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 publicstaticstringGeneratePassword(int length, intnumberOfNonAlphanumericCharacters);//// 摘要://     生成指定长度的随机密码。//// 参数://   numberOfNonAlphanumericCharacters://     生成的密码中的标点字符数。////   length://     生成的密码的字符数。长度必须介于 1 和 128 个字符之间。//// 返回结果://     指定长度的随机密码。



Cases:

1234 for(inti = 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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.