Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - generate random string from letters and digits of size 10

0 contributions
13 points

1. Generate random string with ThreadLocalRandom

public static String randomString(int len) {
	String alphabet =
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
			"abcdefghijklmnopqrstuvwxyz" +
			"0123456789";

	StringBuilder b = new StringBuilder();

	for (int i = 0; i < len; i++) {
		int randIdx = ThreadLocalRandom.current().nextInt(alphabet.length());
		char randChar = alphabet.charAt(randIdx);
		b.append(randChar);
	}

	return b.toString();
}

Example:

System.out.println(randomString(10)); // oBmUYZ3MIg
System.out.println(randomString(10)); // 8gGOjO5Ga2
System.out.println(randomString(10)); // RVrvgA9Okd

Generated random text will contains:

  • upper letters: A-Z
  • lower case letters: a-z
  • digits: 0-9
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