The example in this article describes the method by which Java generates random numbers. Share to everyone for your reference. The implementation method is as follows:
Package Com.tool.code;
Import Java.util.Random; public class Randomnum {private static char ch[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ' , ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' X ', '
Y ', ' z ', ' 0 ', ' 1 '};
Finally, repeat two 0 and 1, because you need to fill the array length for the Random private static Random = new Random ();
Generates a random string of specified lengths public static string createrandomstring (int length) {if (length > 0) {int index = 0;
char[] temp = new Char[length];
int num = Random.nextint ();
for (int i = 0; i < length% 5; i++) {temp[index++] = ch[num & 63];
Take the back six digits, remember that the corresponding binary is in the form of complement. Num >>= 6;//63 binary is: 111111//Why should I move 6 digits to the right?
Because there are 64 valid characters in the array. Why do I have to save 5?
Because an int type is represented by 4 bytes, that is, 32 bits.
for (int i = 0; i < LENGTH/5 i++) {num = Random.nextint (); For(int j = 0; J < 5; J + +)
{temp[index++] = ch[num & 63];
Num >>= 6;
Return the new String (temp, 0, length);
else if (length = = 0) {return "";
else {throw new illegalargumentexception ();
The repeat rate of the random string function is the public static double rateofrepeat (int number) {int repeat=0), based on the specified number;
String[] Str=new String[number];
for (int i=0;i<number;i++) {//Generate a specified number of string str[i]=randomnum.createrandomstring (10); for (int i=0;i<number;i++) {///find if there is the same string for (int j=i+1;j<number-1;j++) {if (Str[i].equals (STR[J)) rep
eat++;
} return (double) repeat)/number; public static void Main (string[] args) {System.out.println (randomnum.createrandomstring (16) + "-" +
Randomnum.createrandomstring (16));
Double Rate=randomnum.rateofrepeat (10000),//test 10,000 times repetition rate System.out.println ("Repetition rate: +rate"); }
}
I hope this article will help you with your Java programming.