Java Program to Generate Random String

You are currently viewing Java Program to Generate Random String

In this tutorial, You will generate a random string using the java program. Yes, I’m going to share the details about random string generator and how to do a task of “Java Program to Generate Random String” here?

The random string generator creates a series of numbers and letters that have no pattern. Random strings can be unique. Okay, Let’s start the tutorial.

Java Program to Generate Random String

Generate a random normal string

We can create a random normal string using the below java program here.

import java.util.Random;

class Main {
  public static void main(String[] args) {

    // create a string of all characters
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // create random string builder
    StringBuilder sb = new StringBuilder();

    // create an object of Random class
    Random random = new Random();

    // specify length of random string
    int length = 9;

    for(int i = 0; i < length; i++) {

      // generate random index number
      int index = random.nextInt(alphabet.length());

      // get character specified by index
      // from the string
      char randomChar = alphabet.charAt(index);

      // append the character to string builder
      sb.append(randomChar);
    }

    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);

  }
}

Explanation

After importing the java.util.Random() class into the program, We have to declare the uppercase alphabets into this program with the string alphabet variable. After that, We can build the new random string using the new StringBuilder() and the new Random() functions respectively.

In addition, We can assign the string length. Later, The for loop iterates from 1 to the string length. After completing all processes, We will get a random normal string output using the random.nextInt() and the alphabet.charAt() functions. The output will display on the screen using the system.out.println() function.

Output

The output will show below for your reference.

Random String is: XBFNHHQMR
Read Also : JavaScript Program to Generate Random String

Java Program to Generate Random String

Generate a random alphanumeric string

Now, The alphanumeric random string will generate by using this below java program.

import java.util.Random;

class Main {
  public static void main(String[] args) {

    // create a string of uppercase and lowercase characters and numbers
    String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lowerAlphabet = "abcdefghijklmnopqrstuvwxyz";
    String numbers = "0123456789";

    // combine all strings
    String alphaNumeric = upperAlphabet + lowerAlphabet + numbers;

    // create random string builder
    StringBuilder sb = new StringBuilder();

    // create an object of Random class
    Random random = new Random();

    // specify length of random string
    int length = 15;

    for(int i = 0; i < length; i++) {

      // generate random index number
      int index = random.nextInt(alphaNumeric.length());

      // get character specified by index
      // from the string
      char randomChar = alphaNumeric.charAt(index);

      // append the character to string builder
      sb.append(randomChar);
    }

    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);

  }
}

Execution

Both two program executions are the same but the only difference is assigning the lowercase alphabets and the numbers along with the uppercase alphabets with variables to generate alphanumeric string.

Output

Below, You can see the reference output.

Random String is: hElp8wNWCGuvJYE

Final Thoughts

I hope this article helps you to learn Java Program to Generate Random String will beneficial for you. Do share this study with other Java learners on social media. If you have any difficulties with this program? ask us via the comment section. Guys, write your feedback about this study with us.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply