Current location - Recipe Complete Network - Complete cookbook - How to generate random numbers and random letters in Java
How to generate random numbers and random letters in Java
Java generates random numbers and random letters, using the madom class. Examples are as follows:

Parcel? com . Qiu . Lin . he;

Public? Class? Shi Ce? {

Public? Static electricity Invalid? main(String[]? args)? {

int? str 1? =? 0;

For what? (int? Me? =? 0; ? Me? & lt? 25; ? i++)? {//? If you want to generate several characters, change 4 to several. If it is changed to 1, a random letter will be generated.

str 1? =? (char)? (Math.random()? *? 26);

}

System.out.println ("output randomly generated strings"? +? str 1);

String? str? =? "";

For what? (int? Me? =? 0; ? Me? & lt? 25; ? i++)? {//? If you want to generate several characters, change 4 to several. If it is changed to 1, a random letter will be generated.

str? =? str? +? (char)? (Math.random()? *? 26? +? a’);

}

System.out.println ("output randomly generated strings"? +? str);

}

} The running results are as follows: