All TalkersCode Topics

Follow TalkersCode On Social Media

Generate Random String In Java

Last Updated : May 17, 2023

Generate Random String In Java

In this article we will show you the solution of generate random string in java, the technique of constructing a string of characters which are randomly chosen from a preset set of characters is known as "generating a random string" in Java.

The string that is created seems random and unpredictable. This is frequently employed in a number of applications, including those that generate passwords, create distinctive IDs, or simulate random data.

Java offers methods like Random and StringBuilder that can be used to accomplish this.

The Random class generates random numbers, & the StringBuilder class creates the random string by inserting characters chosen at random. We'll talk about the Java notion of creating random strings.

Step By Step Guide On Generate Random String In Java :-

import java.util.Random;
class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    int length = 7;
    for(int i = 0; i < length; i++) {
      int index = random.nextInt(alphabet.length());
      char randomChar = alphabet.charAt(index);
    String randomString = sb.toString();
    System.out.println("Random String is: " + randomString);
  1. You can see that we wrote java code to generate random strings here.
  2. We begin our code by using an import line to bring in the java.util library.Java's Random class makes it possible to produce random numbers.
  3. The Java program's entry point is then defined by a class called Main.
  4. The we declare and initialise numerous variables inside the main procedure. All capital alphabets are kept in a string called alphabet.
  5. The random string will be created using the StringBuilder object sb. This will produce random numbers using the random object, which is called random.
  6. The desired random string's desired length, length, is an integer that is set to 7.
  7. The for loop is then used to produce 7 random characters by iterating 7 times (as determined by the length).
  8. The function random.nextInt(alphabet.length()) inside the loop creates a random index between the lengths of the alphabet string.
  9. Then, using the random uppercase alphabet, we use alphabet.charAt(index), which returns the character at the produced index.
  10. The sb StringBuilder object is then appended with this random character using the sb.append(randomChar) method.
  11. When the loop is finished, the desired random string is stored in the sb StringBuilder object.
  12. After that, sb.toString() is used to convert the StringBuilder object into a String object, which is then saved in the randomString variable.
  13. Finally, the created random string and the message showing that it is the "Random String" are printed using System.out.println().

Conclusion :-

Thus, we were able to understand how to produce random strings in Java.

We also discovered that using Java to generate random strings provides a practical method for producing randomised data, such as passwords or distinctive IDs.

To make creating random strings simple and quick, Java offers methods like Random and StringBuilder.

The generated strings can be made to be truly random and unexpected by using these classes, which developers can use.

This method is frequently employed in a number of applications and is still a crucial component of many Java programmes today.

I hope this article on generate random string in java helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials