Languages
[Edit]
EN

Java - generate random char array of size 10

5 points
Created by:
Payne
654

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)));
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - random numbers

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join