randomly generated 5-bit uppercase or lowercase letters or numbers
Method One: Generate a duplicate
public static void Main (string[] args) {
Random rand = new Random ();
char[] letters=new char[]{' 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 ', ' r ', '
0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
String str = "";
int index;
Boolean[] flags = new boolean[letters.length];//defaults to False for
(int i=0;i<5;i++) {
do{
index = Rand.nextint (letters.length);
} while (flags[index]==true);
char C = letters[index];
STR + C;
flags[index]=true;
}
System.out.println (str);
Method Two: Generate duplicates, similar to method one
public static void Main (string[] args) {
Random rand = new Random ();
char[] letters=new char[]{' 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 ', ' r ', '
0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
String str = "";
int index;
Boolean[] flags = new boolean[letters.length];//defaults to False for
(int i=0;i<5;i++) {
do{
index = Rand.nextint (letters.length);
} while (flags[index]==true);
char C = letters[index];
STR + C;
flags[index]=true;
}
System.out.println (str);
Method Three: Generate Duplicates (recommended option)
public static void Main (string[] args) {
String str = "";
Random rand = new Random ();
for (int i=0;i<5;i++) {
int num = rand.nextint (3);
Switch (num) {case
0:
char c1 = (char) (Rand.nextint + ' a ');//Generate random lowercase letter
str + c1;
break;
Case 1:
char c2 = (char) (rand.nextint () + ' A ');//Generate Random uppercase letters
STR + c2;
break;
Case 2:
str = rand.nextint (10);//Generate Random
number
}
System.out.println ("generated 5 random Captcha is:" +str);
}