The example of this article tells the way Java generates random numbers. Share it for everyone for your reference. The specific implementation method is as follows:
package com.Tool.Code; Import java.util.random; Public Class Randomnum {Private Static Char ch [] = {'0', '1', '2', '4', '5', '5', '4', '5', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i i,' i 'J', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',' t ',' u ',' u's 'V', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h,' h 'I', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't' 'u', 'v', 'w', 'x', 'y', 'z', '0', '1'}; // Last two 0 and 1, because you need to make up the length of the array length 64 Private Static Random Random = New Random (); // Generate a random string Public Static String Createndomstring (Int Length) {if (length> 0) {int index = 0; char [] = new char [ length]; int Num = random.nextint (); for (int i = 0; i <langth % 5; i ++) {testx ++] = ch [num & 63]; Binary exists in the form of a supplementary code. NUM >> = 6; // 63's binary is: 111111 // Why do you want to move 6 digits right? Because there are a total of 64 effective characters in the array. // Why do you want to remove the remainder? Because a INT type is represented by 4 bytes, which is 32 -bit. } for (int i = 0; i <langth / 5; i ++) {num = random.nextint (); for (int j = 0; j <5; j ++) {tempx ++] = ch [num & 63] ; Num >> = 6;}} Return New String (Temp, 0, Length);} Else if (length == 0) {Return "";} Else {Throw New illegalargumexception (); Specify Number, test the duplication rate of the random string function Public Static Double Rateofrepeat (int Number) {int Repeat = 0; String [] str = New String [Number]; for (int i = 0; I <number; I ++) {///////////// /Generate the specified number string str [i] = randomnum.createrantomstring (10);} for (int i = 0; i <number; i ++) {// Find whether there are the same string for (int j = i +1; j <number-1; j ++) {if (str [i] .equals (str [j])) repeat ++;}} Return ((dough) repeat); args) {system.out.println (randomnum.createrandomstring (16)+"-"+randomnum.createrantomstring (16)); dough Leading system.out. Println ("Repeat rate:"+Rate);}}
The operating effect of the console is as follows:
It is hoped that this article is helpful to everyone's Java program design.