Skip to content

Create random String in Java example

Create random String in Java example shows how to create random string of a specified length in Java. The example also shows how to create random alphanumeric string, random numeric string or random alphabetic string in Java.

How to create a random string of the specified length in Java?

There are several ways in which you can create a random string in Java as given below.

1) Using the Random and String classes

We can create a random string of specified length using the java.util.Random class as given below.

Output

We kept all the allowed characters in a String for later reference and took one random character from it and appended it to the StringBuilder object. When we had required string length, we converted StringBuilder to String and returned it.

You can change the String containing allowed characters according to your needs. You can also change the string required length to generate a random string of specified characters.

Note: Please note that the above solution uses java.util.Random class. If you want a secure solution change the Radom with SecureRandom class.

If you are using Java 1.4 or earlier version, use the StringBuffer class instead of the StringBuilder class.

2) Using the SecureRandom and BigInteger classes

The BigInteger class can be used to generate random strings by using below given constructor.

This constructor creates randomly generated non-negative BigInteger in the range of 0 to 2^bits – 1. Here is the example program to generate random string using BigInteger and SecureRandom classes.

Output

3) Using the UUID class

Java provides UUID (Universally unique identifier) class which can be used to generate random string. Use randomUUID method of the UUID class to generate random 128-bit string.

This method generates level 4 (i.e. pseudo randomly generated) UUID.

What is version 4 UUID?

Version 4 UUID is based on random numbers. Algorithm of version 4 UUID keeps version number and other 2 bits reserved. Every other bits are set by random numbers. Below given is the format of version 4 UUID.

Where x could be any hexadecimal digit. Y is any one of 8, 9, a or b.

Here is the example program to generate random level 4 UUID.

Output

If you don’t want “-” in the final strings, you can change the line which generates the UUID as given below.

4) Using Apache Commons

If you are using the Apache Commons library, you can use the RandomStringUtils class from Apache Commons lang to generate random string of specified length.

a) Generate random alphanumeric string of specified length

Use randomAlphanumeric method of RandomStringUtils class to generate random alphanumeric string.

This method generates a random alphanumeric string of specified length.

Output

b) Generate a random alphabetic string of specified length

Use randomAlphabetic method of RandomStringUtils class to generate random alphabetic string.

This method generates a random alphabetic string of specified length.

Output

c) Generate a random alphanumeric string of specified length

Use randomNumeric method of RandomStringUtils class to generate random numeric string.

This method generates a random numeric string of specified length.

Output

Note: Please note that the RandomStringUtils class uses java.util.Random to produce random sequences. Use the SecureRandom class along with random method of the RandomStringUtils class to generate secure randoms as given below.

Where,
count – number of characters
start – start index position in the source char[]
end – end index position in the source char[]
letters – true if letters are allowed
numbers – true if numbers are allowed
char[] – char array to choose random letters from
random – the source of random sequence

Example program

Output

This example is a part of the Java String tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.