The core trick in Java is picking a random offset from 'A' and casting it to a char. Here's the standard approach, a secure version, and a thread-safe version. After that, sections 6–9 show how to generate a random string in Java: letters only, alphanumeric, with streams, and with Apache Commons.
1. Using java.util.Random
import java.util.Random; Random random = new Random(); char letter = (char) ('A' + random.nextInt(26)); System.out.println(letter);nextInt(26) returns 0–25, and adding that offset to 'A' (65 in ASCII) lands anywhere from A to Z.
2. The secure way: SecureRandom
If the letter feeds into a token, password component, or verification code, use SecureRandom instead of Random — it's built for cryptographic use and isn't predictable the same way java.util.Random can be.
import java.security.SecureRandom; SecureRandom secureRandom = new SecureRandom(); char letter = (char) ('A' + secureRandom.nextInt(26)); System.out.println(letter);3. Using ThreadLocalRandom (better for concurrent code)
If you're generating letters from multiple threads, ThreadLocalRandom avoids contention that a shared Random instance can cause:
import java.util.concurrent.ThreadLocalRandom; char letter = (char) ('A' + ThreadLocalRandom.current().nextInt(26)); System.out.println(letter);4. Lowercase or mixed case
char lower = (char) ('a' + random.nextInt(26)); // mixed case boolean upper = random.nextBoolean(); char mixed = upper ? (char) ('A' + random.nextInt(26)) : (char) ('a' + random.nextInt(26));Flipping a coin per character like this is also the basic idea behind a random capitalization generator, just applied to existing text instead of freshly generated letters.
5. Restricting to vowels or a custom set
String vowels = "AEIOU"; char randomVowel = vowels.charAt(random.nextInt(vowels.length())); System.out.println(randomVowel);
6. Generating a random string of letters
A random string is just a random letter in a loop:
StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5; i++) {sb.append((char) ('A' + random.nextInt(26)));}System.out.println(sb.toString());7. Random alphanumeric string generator
For IDs, test data, or coupon codes you usually want letters and digits. Put the allowed characters in a string and pick from it. Use SecureRandom if the string works as a password, token, or anything else that must be hard to guess:
import java.security.SecureRandom; public class RandomStringGenerator {private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; private static final SecureRandom RANDOM = new SecureRandom(); public static String randomString(int length) {StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) {sb.append(CHARS.charAt(RANDOM.nextInt(CHARS.length())));}return sb.toString();}public static void main(String[] args) {System.out.println(randomString(12)); // e.g. "k7GqP2xZm9Ab"}}8. Random string with Java streams (Java 8+)
import java.util.Random; import java.util.stream.Collectors; String random = new Random().ints(10, 0, 26) .mapToObj(i -> String.valueOf((char) ('a' + i))) .collect(Collectors.joining()); System.out.println(random); // e.g. "qwhzbtkera"9. Using Apache Commons Lang
If your project already depends on Apache Commons Lang 3, RandomStringUtils does it in one line:
import org.apache.commons.lang3.RandomStringUtils; String letters = RandomStringUtils.randomAlphabetic(8); String alnum = RandomStringUtils.randomAlphanumeric(12); // For security-sensitive values, prefer RandomStringUtils.secure() (Lang 3.16+)
Random string vs. UUID
If all you need is a unique ID, UUID.randomUUID().toString() is built in and needs no character set. Use a custom random string generator like the one above when you need to control the length, the characters, or the readability, for example to avoid look-alike characters such as 0/O and 1/l.
Prefer not to write the code?
The random letter generator on this site covers all of this through a UI without writing the loop yourself.