C #. Net random generate random numbers and letters

Source: Internet
Author: User

1: random number generator

ClassRandomIs A. Net pseudo-random number generator. To generate various types of random numbers, you must first obtain its instance object and then generate a random number.

2: Seeds

The random number is generated starting from the seed value. If the same seed is used repeatedly, the same digital series will be generated. One way to produce different sequences is to make the seed value related to time.

3: object instance

By default, the nonparametric constructor of the random class uses the system clock to generate its seed value.

The parameterized constructor provides an int32 type number as the starting value.

4: generate a solution

Solution 1: instantiate only one object and call Methods multiple times

Random RND = new random (); int I1 = RND. next (10); int I2 = RND. next (10); // simple, convenient, and commonly used. The generated numbers are evenly distributed and each number returns an equal probability. // Generally, the instance is a static object to reduce the number of Instantiation times and avoid the same seed value, as shown below:
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();

Solution 2: Each method call uses a new instance (No parameter)

Random rnd1 = new random (); int I1 = rnd2.next (10); random rnd2 = new random (); int I2 = rnd2.next (10 ); // two random numbers are the same in. // The default seed value is derived from the system clock and has a limited resolution. // Therefore, different objects that are frequently created by calling the default constructor will have the same default seed value, and several groups of identical random numbers will be generated.

Solution:

Blocking the current thread for a short time

Random rnd1 = new Random();System.Threading.Thread.Sleep(500);Random rnd2 = new Random();

Or, use an algorithm to differentiate the seed values of each call.

For example, the following code uses the right shift operator to generate different seed values for multiple random objects that can be initialized with the same time value (between 1 and about 28 objects.

int count = 4;Random[] rnds = new Random[count];for (int i = 0; i < count; i++){    rnds[i] = new Random(unchecked((int)(DateTime.Now.Ticks >> i)));}

5: random generation of General numbers and letters

Array Method: omitted

String mode: not flexible, but easy to use. It is sufficient for verification codes.

String STR = @ "0123456789 abcdefghigklmnopqrstuvwxyzabcdefghigklmnopqrstuvwxyz"; Public String getmix (random RND) {// return a number // return RND. next (10 ). tostring (); // return lowercase letters // return Str. substring (10 + RND. next (26), 1); // return uppercase letters // return Str. substring (36 + RND. next (26), 1); // returns a mix of uppercase and lowercase letters // return Str. substring (10 + RND. next (52), 1); // return lowercase letters and numbers. // return Str. substring (0 + RND. next (36), 1); // returns a mix of uppercase letters and numbers // return Str. substring (0 + RND. next (36), 1 ). toupper (); // return a mix of uppercase and lowercase letters and numbers. Return Str. substring (0 + RND. next (61), 1 );}

6: Fill in the array of specified bytes with a random number

Random RND = new random (); // custom array Length byte [] BS = new byte [9999]; // each element of the byte array is set to a random number greater than or equal to zero and less than or equal to maxvalue. Maxvalue = 255, the upper bound value RND. nextbytes (BS) is recommended );

7: generate a double-precision floating point number greater than or equal to 0.0 and less than 1.0

Random RND = new random (); // double-precision floating point number greater than or equal to 0.0 and less than 1.0. Double D = RND. nextdouble (); // format to 5 decimal places. Of course, there are more types of formatting string S = string. Format ("{0: F5}", d );

8: generate a random integer

Random RND = new random (); // a 32-bit signed integer greater than or equal to zero and less than maxvalue. Maxvalue = 2,147,483,647, cannot take the upper bound value int I1 = RND. next (); // a 32-bit signed integer greater than or equal to zero and less than maxvalue. The upper bound value int I2 = RND cannot be obtained. next (10); // a 32-bit signed integer greater than or equal to minvalue and smaller than maxvalue. The upper bound value cannot be used. A negative value int I3 = RND can be used. next (-1, 10,100 );

9: generate random uppercase letters

Public String getuppercase (random RND) {// A-Z ASCII value 65-90 int I = RND. next (65, 91); char c = (char) I; return C. tostring ();}

10: Generate random lowercase letters

Public String getlowercase (random RND) {// A-z ascii value 97-122 int I = RND. next (97,123); char c = (char) I; return C. tostring ();}

11: generate a random mix of uppercase and lowercase letters

Public String getletter (random RND) {// A-Z ASCII value 65-90 // A-z ascii value 97-122 int I = RND. next (65,123); char c = (char) I; If (char. isletter (c) {return C. tostring ();} else {// recursive call until random return getletter (RND );}}

12: generate a mix of random uppercase/lowercase letters and numbers

Public String getchar (random RND) {// 0-9 // A-Z ASCII value 65-90 // A-z ascii value 97-122 int I = RND. next (0,123); if (I <10) {// return the number return I. tostring () ;}char c = (char) I; // return lowercase letters with digits // return char. islower (c )? C. tostring (): getchar (RND); // returns an uppercase letter with a number. // return Char. isupper (c )? C. tostring (): getchar (RND); // return uppercase/lowercase letters and numbers. Return Char. islower (c )? C. tostring (): getchar (RND );}
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.