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 back six digits, remember that the corresponding binary system 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[nu M & 63]; Num >>= 6; Return the new String (temp, 0, length); else if (length = = 0) {return "";} else {throw new IllegalArgumentException ();}} Tests the repeat rate of a random string function, based on the number of specified numbers, the public static double rateofrepeat (int number) {int repeat=0; String[] Str=new String[number]; for (int i=0;i<number;i++) {//Generate a specified number of strings str[i]=randomnum.createrandomstring;} 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))) repeat++;} 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 repeat rate System.out.println ("repeat rate: +rate);}" |