In this Java tutorial, I am going to share with you different ways how to generate a random string of characters. You will learn to generate the following types of random Strings of characters:
- alpha-numeric String of characters of any length or of a specific length,
- Letters only,
- Numbers only.
I hope you will find this tutorial useful.
Generate a Random String Using UUID
One of the easiest and quickest ways to generate a random alpha-numeric String of characters is probably to use the UUID, which stands for a Universal Unique Identifier and is a 128-bit number used to uniquely identify some object or entity on the Internet.
The String is generated using a cryptographically strong pseudo-random number generator. For this code to work, you do not need to add to your project any external third-party libraries.
Here is a complete example you can copy and run on your computer.
import java.util.UUID; public class UUIDExample { public static void main(String[] args) { String uuid = UUID.randomUUID().toString(); System.out.println(uuid); } }
The output is 36 characters long: 5d87ab34-f9c1-4424-851c-58c9eddf21ff
You can also remove the dashes from the resulting String in the following way:
import java.util.UUID; public class UUIDExample { public static void main(String[] args) { String uuid = UUID.randomUUID().toString(); System.out.println(uuid + " of length " + uuid.length()); // Remove dashes String uuid2 = uuid.replaceAll("-", ""); System.out.println(uuid2); } }
The output is 32 characters long: 5d87ab34f9c14424851c58c9eddf21ff
Generate a Random String of Any Length
The below example generates a random alpha-numeric String of characters of a specific length. You can use it to generate a random string of characters of any length you need. For this code to work, you do not need to add to your project any external third-party libraries.
public class RandomStringExample { private final Random RANDOM = new SecureRandom(); private final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; public static void main(String[] args) { RandomStringExample randomStringExample = new RandomStringExample(); String randomString = randomStringExample.generateRandomString(30); System.out.println(randomString); } private String generateRandomString(int length) { StringBuilder returnValue = new StringBuilder(length); for (int i = 0; i < length; i++) { returnValue.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length()))); } return new String(returnValue); } }
The output is 30 characters long, but it could be 130 characters long: niq4ZtRZdN7rfSu81dyqy0oYhq7mmc
Random String of Any Length with Apache Commons
The below code examples uses Apache Commons Lang library to generate a random alpha-numeric string of characters. The advantage of this approach is that you can generate different types of random Strings:
- Generate a Random String of any length,
- Alpha-numeric String,
- A string of letters only,
- A string of numbers only.
Which is pretty cool.
package codeexamples; import org.apache.commons.lang3.RandomStringUtils; public class RandomStringWithApache { public static void main(String[] args) { int length = 30; boolean includeLetters = true; boolean includeNumbers = true; String randomString = RandomStringUtils.random(length, includeLetters, includeNumbers); System.out.println(randomString); } }
If you are interested in learning more about Java and like learning by watching a series of step-by-step video tutorials, then have a look at the below list of online video courses that teach Java.
You will also find a lot of free video tutorials on this site. For example, check out this section with Spring Boot video tutorials for beginners.