EN
Java - generate random char array of size 10
5
points
1. Generate random char array of size 10 with Random
public static char[] randomCharArray(int len) {
String alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
StringBuilder b = new StringBuilder();
for (int i = 0; i < len; i++) {
int randIdx = new Random().nextInt(alphabet.length());
char randChar = alphabet.charAt(randIdx);
b.append(randChar);
}
return b.toString().toCharArray();
}
Example:
// [v, K, 5, f, H, R, k, j, O, K]
System.out.println(Arrays.toString(randomCharArray(10)));
// [h, U, x, V, u, P, C, c, 1, I]
System.out.println(Arrays.toString(randomCharArray(10)));
// [F, Z, p, i, 0, d, O, X, a, B]
System.out.println(Arrays.toString(randomCharArray(10)));