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
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 8 )
2 weeks ago - 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 › 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:
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 7 )
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
🌐
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
Find elsewhere
🌐
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 ...
🌐
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.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › RandomStringUtils.html
RandomStringUtils (Apache Commons Lang 3.20.0 API)
Generates random Strings. Use secure() to get the singleton instance based on SecureRandom() which uses a secure random number generator implementing the default random number algorithm.
🌐
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 ...
🌐
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.
🌐
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.
🌐
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 ...
🌐
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 ...
🌐
sqlpey
sqlpey.com › java › generate-random-strings-java
Generate Random Strings in Java: Practical Approaches and Best Practices
July 22, 2025 - This method provides a highly flexible way to generate random strings by concatenating characters randomly selected from a user-defined set of symbols. import java.util.Random; import java.util.Objects; import java.util.concurrent.ThreadLocalRandom; import java.security.SecureRandom; public ...
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › MSC02-J.+Generate+strong+random+numbers
MSC02-J. Generate strong random numbers - SEI CERT Oracle Coding Standard for Java - Confluence
import java.util.Random; // ... ... import java.security.NoSuchAlgorithmException; // ... public static void main (String args[]) { SecureRandom number = new SecureRandom(); // Generate 20 integers 0..20 for (int i = 0; i < 20; i++) { System.out.println(number.nextInt(21)); ...
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › java-string-random-your-ultimate-guide-to-generating-random-strings-in-java-3ona
Java String Random(): Your Ultimate Guide to Generating Random Strings in Java - DEV Community
December 7, 2025 - Conclusion So, there you have it. Generating random strings in Java isn't just one thing—it's a toolkit. From the simple Math.random() to the robust SecureRandom and the uniquely-focused UUID, you've now got a clear map of when and how to use each.