This post will discuss how to generate a random character in Java.

1. Using Random.nextInt() method

If you need to generate a random character between some specific range, you can do so using the nextInt() method from the Random class. The following code generates a random character in the range A-Z. You can easily modify the code to generate a character in the range a-z.

Download  Run Code

 
If you need to generate values in the ranges a-z and A-Z, you can do something like:

Download  Run Code

 
If you want to generate an alphanumeric random character or use any other specific character set, you can do as follows.

Download  Run Code

2. Using Apache Commons Lang

You could also use the RandomStringUtils class from the Apache Commons Lang library. It offers several methods like randomAlphabetic(), randomAlphanumeric(), randomNumeric(), randomAscii(), etc., to create a random string of specific length.

Download Code

 
To get the lowercase equivalent, call the toLowerCase() method:

Download Code

That’s all about generating a random character in Java.