Initialize an array containing all the accepted chars (CHARS_ARRAY), then instantiate a SecureRandom instance, and call nextInt(CHARS_ARRAY.length) repeatedly to get a random index in your char array. Append each char to a StringBuilder until you get the expected number of chars.

Answer from JB Nizet on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 8 )
1 week ago - See the SecureRandom section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard RNG algorithm names. The returned SecureRandom object has not been seeded. To seed the returned object, call the setSeed method. If setSeed is not called, the first call to nextBytes will force the SecureRandom object to seed itself. This self-seeding will not occur if setSeed was previously called. ... Constructs a secure random number generator (RNG) implementing the default random number algorithm.
🌐
Baeldung
baeldung.com › home › java › java string › java – generate random string
Java - Generate Random String | Baeldung
May 11, 2024 - Through different implementation methods, we were able to generate bound and unbound strings using plain Java, a Java 8 variant, or the Apache Commons Library. In these Java examples, we used java.util.Random, but one point worth mentioning is that it is not cryptographically secure.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 7 )
See the SecureRandom section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard RNG algorithm names. The returned SecureRandom object has not been seeded. To seed the returned object, call the setSeed method. If setSeed is not called, the first call to nextBytes will force the SecureRandom object to seed itself. This self-seeding will not occur if setSeed was previously called. ... Constructs a secure random number generator (RNG) implementing the default random number algorithm.
🌐
GitHub
github.com › amdegregorio › SecureRandomStringGenerator
GitHub - amdegregorio/SecureRandomStringGenerator: A simple utility for generating secure random strings using Java's java.util.SecureRandom class. · GitHub
The Secure Random String Generator is a simple utility for using Java's java.security.SecureRandom to generate secure random strings.
Author   amdegregorio
🌐
Java67
java67.com › 2018 › 01 › how-to-create-random-alphabetic-or-alphanumeric-string-java.html
How to Create Random Alphabetic or AlphaNumeric String of given length in Java? SecureRandom Example | Java67
In order to generate a random alphabetic String of a given length, we first create a source String by concatenating all possible characters as shown below: static final String SOURCE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; And then ...
🌐
Mkyong
mkyong.com › home › java › java – how to generate a random string
Java - How to generate a random String - Mkyong.com
May 4, 2019 - 1.1 Generate a random alphanumeric String [a-ZA-Z0-9], with a length of 8. ... package com.mkyong; import java.security.SecureRandom; public class RandomExample { private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private ...
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › generating-random-password-strings-in-java-for-beginners-98b5d78a58e3
Generating Random Password Strings in Java for Beginners
September 22, 2025 - That’s why StringBuilder is the go-to for this type of generator. Controlling how long a password is comes down to looping a fixed number of times. Each pass picks a random index from the pool and appends the matching character.
🌐
Quickprogrammingtips
quickprogrammingtips.com › java › random-alphanumeric-generator-in-java.html
Random Alphanumeric Generator in Java
If cryptographic security is not a requirement, the SecureRandom class can be replaced with Random class for a much faster implementation. The following function can be used to generate random strings with a specified length. It is also possible to specify a subset of character set in the random string by modifying the CHARACTER_SET variable. import java.security.SecureRandom; // Example - Random alphanumeric generator in Java public class RandomAlphaNumericGenerator { private static SecureRandom random = new SecureRandom(); private static final String CHARACTER_SET="0123456789abcdefghijklmnop
🌐
Baeldung
baeldung.com › home › java › generate a secure random password in java
Generate a Secure Random Password in Java | Baeldung
January 8, 2024 - On a side note, Apache advocates the usage of RandomStringUtils for simple use cases only. We can also make use of the SecureRandom class to create a custom utility class for our scenario. For starters, let’s generate a string of special characters of length two:
🌐
javaspring
javaspring.net › blog › how-to-generate-a-securerandom-string-of-length-n-in-java
How to Generate a SecureRandom String of Specified Length (n) in Java: Fixing Short Random Strings Issue — javaspring.net
Java’s `SecureRandom` class is the gold standard for generating cryptographically secure random numbers, but developers often encounter a frustrating issue: the generated string is shorter than the specified length (`n`). This blog demystifies why this "short string" problem occurs and provides ...
🌐
Chrysanthium
chrysanthium.com › java-generate-secure-random-string.html
Java: Generate Secure Random String || Chrysanthium
private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; private static final String NUMBERS = "0123456789"; // https://www.owasp.org/index.php/Password_special_characters private static final String SPECIAL = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; private static final String ALL = UPPERCASE + LOWERCASE + NUMBERS + SPECIAL; To select random tokens from the sets we will use the SecureRandom implementation as it is a cryptographically strong random number generator:
🌐
javaspring
javaspring.net › blog › get-random-string-java
Generating Random Strings in Java — javaspring.net
The Random class is used to generate a random index within the range of the character set length. We then append the character at that index to a StringBuilder and return the final string. import java.security.SecureRandom; public class ...
🌐
Piotr Horzycki
peterdev.pl › secure-generation-of-random-ids-and-passwords
Secure generation of random IDs and passwords in Java | Piotr Horzycki - Java and PHP developer’s blog
January 10, 2021 - Tools which provide safe, unpredictable random numbers and strings in your Java application: SecureRandom, Apache Commons and Passay.
🌐
Kodejava
kodejava.org › how-do-i-generate-random-string
How do I generate random string? - Learn Java by Examples
July 4, 2023 - package org.kodejava.security; import java.security.SecureRandom; import java.util.Random; public class RandomString { public static final String SOURCES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; public static void main(String[] args) { RandomString rs = new RandomString(); System.out.println(rs.generateString(new Random(), SOURCES, 10)); System.out.println(rs.generateString(new Random(), SOURCES, 10)); System.out.println(rs.generateString(new SecureRandom(), SOURCES, 15)); System.out.println(rs.generateString(new SecureRandom(), SOURCES, 15)); } /** * Generate a random string.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › security › SecureRandom.html
SecureRandom (Java SE 11 & JDK 11 )
January 20, 2026 - See the SecureRandom section in the Java Security Standard Algorithm Names Specification for information about standard RNG algorithm names. ... Constructs a secure random number generator (RNG) implementing the default random number algorithm.
🌐
Baeldung
baeldung.com › home › security › the java securerandom class
The Java SecureRandom Class | Baeldung
February 20, 2025 - In this short tutorial, we’ll learn about java.security.SecureRandom, a class that provides a cryptographically strong random number generator.
🌐
Our Code World
ourcodeworld.com › articles › read › 964 › how-to-generate-random-alphanumeric-strings-with-a-custom-length-in-java
How to generate random alphanumeric strings with a custom length in Java | Our Code World
June 14, 2019 - In order to generate a random string with a custom implementation, you can use the following method as helper in your own project: import java.security.SecureRandom; /** * This method returns a random string generated with SecureRandom * * @param ...