Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is RandomStringUtils.randomAscii() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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

Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).

There are two variations of this method:

Variation 1

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

Syntax


public static String randomAscii(final int count)

Parameters

final int count: the length of random string to generate.

Return value

The method returns a random string.

Variation 2

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

Syntax


public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive)

Parameters

final int minLengthInclusive: The inclusive minimum length of the string to generate.

final int maxLengthExclusive: The exclusive maximum length of the string to generate.

Return value

The method returns 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

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 3]q7Y of length five.

Example 2

  • minimum length = 5
  • maximum length = 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 -c<Qe? of length six.

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

Output

The output of the code will be as follows:


The output of RandomStringUtils.randomAscii when the length is 5 - 3]q7Y
The output of RandomStringUtils.randomAscii when the (minlength, maxlength) is (5, 10) - -c<Qe? 

The output might differ each time the code above is run.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring