What is RandomStringUtils.randomGraph() in Java?

The randomGraph() method is a staticthe methods in Java that can be called without creating an object of the class. method of the RandomStringUtils class which is used to generate a random string consisting of visible ASCII characters.

Characters are chosen from a set of characters that match the POSIX [:graph:] regular expression character class. This class contains all visible ASCII characters (i.e., anything except spaces and control characters).

There are two variations to this method.

Variation 1

Syntax

This variation of the method creates a random string whose length is the number of characters specified.

public static String randomGraph(int count)

Parameters

count: the length of random string to generate.

Variation 2

Syntax

This variation of the method creates a random string whose length is between the inclusive minimum and the exclusive maximum.

public static String randomGraph( int minLengthInclusive, int maxLengthExclusive)

Parameters

  • minLengthInclusive: The inclusive minimum length of the random string to create.
  • maxLengthExclusive: The exclusive maximum length of the string to create.

Return Value

Both variants of this method return a random string.

How to import RandomStringUtils

The definition of RandomStringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the RandomStringUtils class as follows:


import org.apache.commons.lang3.RandomStringUtils;

Code

import org.apache.commons.lang3.RandomStringUtils;
public class Main{
public static void main(String[] args){
int count = 5;
System.out.println("The output of RandomStringUtils.randomGraph when the length is " + count + " - " + RandomStringUtils.randomGraph(count));
int minLength = 5;
int maxLength = 10;
System.out.println("The output of RandomStringUtils.randomGraph when the (minlength, maxlength) is (" + minLength + ", " + maxLength + ") - " + RandomStringUtils.randomGraph(minLength, maxLength));
}
}

Output

The output of the code will be as follows:


The output of RandomStringUtils.randomGraph when the length is 5 - J#F)-
The output of RandomStringUtils.randomGraph when the (minlength, maxlength) is (5, 10) - <QbhX89_D

Note: The output might differ every time the code is run.

Explanation

Example 1

  • count = 5

In the first example, we use the first variation of the method where we specify the exact length of the generated string. The method generates the random string, J#F)- with a length of five, consisting of characters belonging to the regex class [:graph:].

Example 2

  • minLength = 5
  • maxLength = 10

In the second example, we use the second variation of the method where we specify the minimum and maximum length of the generated string. The method generates the random string, <QbhX89_D with a length of nine, consisting of characters belonging to the regex class [:graph:].

Free Resources

Attributions:
  1. undefined by undefined