java之隨機字符
阿新 • • 發佈:2018-02-07
println package length str pre oid imp har con package Shoot;
import java.util.Arrays;
import java.util.Random;
public class Generate {
public static void main(String[] args) {
char [] b =generate();
System.out.println(Arrays.toString(b));
}
public static char[] generate() {
Random ran=new Random();
char[] letter = new char[26];
for(int i = 0;i<letter.length;i++) {
letter[i]=(char) (65+i);
}
int index = 0;
int i = 0;
boolean[] flag = new boolean[letter.length];
//System.out.println(Arrays.toString(flag));
char [] ch = new char[4];
while(index<ch.length) {
i = ran.nextInt(letter.length);
if(flag[i]) {
continue;
}
//i = ran.nextInt(letter.length);
//i=(int)(Math.random()*27);
ch[index]=letter[i];
flag[i]=true;
index++;
}
return ch;
}
}
java之隨機字符