Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - generate random char array of size 10

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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more