Java String Handling Java Util

Create random String in Java example

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

How to create random string of specified length in Java?

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

1) Create random string in Java using Random and String classes

We can create a random string of specified length using 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 random string of specified characters.

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

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

2) Create random string in Java using SecureRandom and BigInteger classes

BigInteger 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) Create random string in Java using UUID class

Java provides UUID (Universally unique identifier) class which can be used to generate random string. Use randomUUID method of 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 the 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) Create random string in Java using Apache Commons

If you are using Apache Commons library, you can use 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 random alphanumeric string of specified length.

Output

b) Generate random alphabetic string of specified length

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

This method generates random alphabetic string of specified length.

Output

c) Generate random alphanumeric string of specified length

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

This method generates random numeric string of specified length.

Output

Note: Please note RandomStringUtils class uses java.util.Random to product random sequences. Use SecureRandom class along with random method of 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 – source of random sequence

Example program

Output

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

 

Join 1000+ fellow learners! Enter your email address below: