Languages
[Edit]
EN

Java - generate random char array of size 10

5 points
Created by:
AnnLen
9180

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)));

Java - random numbers

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join