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

13 points
Created by:

1. Generate random string with ThreadLocalRandom

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

	StringBuilder b = new StringBuilder();

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

	return b.toString();


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

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍