Math. Random ()

Source: Internet
Author: User

Math. Random ();
Math. Random () is a pseudo-random double value that allows the system to randomly select a value greater than or equal to 0.0 and less than 1.0)

Returns the formula of the random number [M-N) in the specified range: Math. Random () * (N-m) + m;
Returns the formula of the random number [M-N] (with different intervals) in the specified range: Math. Random () * (N-m + 1) + m;

Eg:
1>: generate a 6-digit random string:
Public static void main (string [] ARGs ){
String result = "";
For (INT I = 0; I <6; I ++ ){
// Generate an integer of the int type 97-122 [97,122]
Int intvalue = (INT) (math. Random () * 26 + 97 );
// Forcibly convert intvalue to char type before receiving resul
Result = Result + (char) intvalue;
}
// Output string
System. Out. println (result );
}
2>: Use the random () method of the math class to generate a character. If the character is an uppercase English letter, enter "Yes! ", Otherwise," no "is output ".
The random number generated by the random () method is between 0.0 and 1.0, multiplied by 128, and its value is between 0.0 and 128.0. After converting it to the char type, use if to determine whether it is between 'A' and 'Z. The procedure is as follows:
Class isupper {
Public static void main (string [] ARGs ){
Char ch;
Ch = (char) (math. Random () * 128 );
If (CH> = 'A' & Ch <= 'Z ')
System. Out. println ("Yes! ");
Else
System. Out. println ("No! ");
}
}

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.