Java Generate Random String

In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any pattern. Thus, no one can guess which string is going to be generated. Some of the ways to generate the random string are discussed below.

By Using the random() Method

The Math class has the static method random(), which returns a double value between 0.0 and 1.0.

Syntax:

The following program uses the random() method to generate the random strings.

FileName: RandomStringsExample.java

Output:

                                                                                                             
Explanation: The strings are generated from picking the characters at random from the alphNumericStr. The size of the randomly generated string is also dependent on the random() method. Therefore, we see different sizes of string in the output. Note that every time we run the above program, different strings of different lengths are generated.

By Using CharSet

CharSet can be used to generate random strings of the random size. The CharSet is present in the java.nio.charset package.

FileName: RandomStringsExample1.java

Output:

Explanation: First, we have bounded that the length of the byte array is 256. It means that a string rndmString comprising 256 characters is generated. From this string, alphanumeric characters are filtered with the help of the for-loop. These filtered alphanumeric characters comprise the string str, which is displayed in the output. The blank spaces in the output represent that the for-loop did not encounter any alphanumeric character.

By Using Regular Expressions

Regular expressions can also be used to generate random strings. The following program illustrates the same.

FileName: RandomStringsExample2.java

Output:

Explanation: The above program is quite similar to the previous one. The only difference is that the usage of the regular expression. Instead of filtering the alphanumeric characters from the string using if-block, we have used the regular expression to eliminate the non-alphanumeric characters from the string.

By Using ASCII Values

The random strings can also be generated using the ASCII values of characters. In this approach, the random() method points to the ASCII value of the character. Using the ASCII value, the character is retrieved and added to the string. The string is then returned. Let’s observe the following example for a better understanding.

FileName: RandomStringsExample3.java

Output:

Explanation: The random() method has been used in such a way that it always generates numbers ranging between 65 – 90, 97 – 122, and 48 – 57. These ranges are nothing but the ASCII value of capital letters, small letters, and digits, respectively. The choice of selection among the capital letters or small letters or digits is also dependent on the random() method. The variable randInt4 determines whether the next character is going to be a capital letter, or smaller letter, or the digits. The randInt4 always gives a random value between 0 to 2, where 0 maps to capital letters, 1 map to small letters, and the 2 maps to digits.

Pin It on Pinterest

Share This