Multiple Random generation methods

Source: Internet
Author: User

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: 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 threshold value is acceptable

Rnd. nextbytes (bs );

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

Random rnd = new random ();

// A 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 formats

String s = string. format ("{0: f5}", d );

7: 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 cannot be used.

Int i2 = rnd. next (10 );

// A 32-bit signed integer greater than or equal to minvalue and less than maxvalue. The upper bound value cannot be used and a negative value can be used.

Int i3 = rnd. next (-10,100 );

8: 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 ();

}

9: 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 ();

}

10: 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 to letter

Return getletter (rnd );

}

}

11: 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 a number

Return I. tostring ();

}

Char c = (char) I;

// Return lowercase letters and numbers

// Return char. islower (c )? C. tostring (): getchar (rnd );

// Return uppercase letters and numbers

// Return char. isupper (c )? C. tostring (): getchar (rnd );

// Return uppercase/lowercase letters and numbers

Return char. islower (c )? C. tostring (): getchar (rnd );

}

12: other random generation schemes

Array Method: omitted

String mode:

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 the combination of uppercase letters and numbers.

// Return str. substring (0 + rnd. next (36), 1). toupper ();

// Returns a mix of uppercase and lowercase letters and numbers.

Return str. substring (0 + rnd. next (61), 1 );

}

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.