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! ");
}
}