Three ways to generate 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.

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

 for  (int0; i++)    {new Random (); // no parameter is used to seed the system clock     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.

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)

New RNGCryptoServiceProvider (); byte New byte [ten];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.

 for (int0; i++)    {new  RNGCryptoServiceProvider ();     byte New byte [ten];    Csp. GetBytes (BYTECSP);    Console.WriteLine (Bitconverter.tostring (BYTECSP));}
但是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.

 Public Static stringGeneratepassword (intLengthintnumberofnonalphanumericcharacters);////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 of the generated password. The length must be between 1 and 128 characters. ////return Result://Specifies the length of the random password. 

Cases:

 for (int0; i++) {    Response.Write (Membership.generatepassword ( 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*%

I wonder if it just fits your request?

Three ways to generate random numbers in C #

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.