Generate Random Strings in Java

Exercise:

Write a Java Program to create random strings.

1.Generate random alphabetic string

Click Here to View the Solution!
import java.util.Random;

public class RandomString {
    public static void main(String[] args) {
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder();
        
        // create an object of Random class
        Random random = new Random();

        // specify length of random string
        int length = 5;
        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);
    }
}
Click Here to View the Output!
Random String is: DZTSW 
Click Here to View the Explanation!
  • In this program, first a string type variable named alphabet is initialized equal to the whole set of uppercase alphabets from A-Z.
  • Another variable called length is initialized equal to 5. This variable determines the length of random string which is to be generated.
  • A for loop then executes till i is less than the value of length.
  • In this loop, first a random number is retrieved from the range of index numbers of the alphabet’variable. This number is stored in the variable index.
  • Then a character from alphabet string is retrieved according to the random number stored in index. This character is stored in randomChar variable.
  • Lastly, this character in randomChar is appended to the string builder object sb.
  • When the loop breaks, all the appended characters are displayed on the screen.

2.Generate random alpha-numeric string

Click Here to View the Solution!
import java.util.Random;

public class Random {
    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";

        // combining all strings
        String alphaNumeric = upperAlphabet + lowerAlphabet + numbers;
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        int length = 11;
        for(int i = 0; i < length; i++) {
            int index = random.nextInt(alphaNumeric.length());

            // get character specified by index
            char randomChar = alphaNumeric.charAt(index);
            sb.append(randomChar);
        }
        String randomString = sb.toString();
        System.out.println("Random String is: " + randomString);
    }
}
Click Here to View the Output!
Random String is: ZK2darAoAfl
Click Here to View the Explanation!
  • In this program, firstly three variables are initialized as the upper-case alphabets, lower case alphabets and numbers respectively.
  • The values of all these variables are concatenated and stored in a variable called alphaNumeric.
  • Another variable called length is initialized equal to 11. This variable determines the length of random string which is to be generated.
  • A for loop then executes till i is less than the value of alphanumeric variable’s length.
  • In this loop, first a random number is retrieved from the range of index numbers of the alphaNumeric variable. This number is stored in the variable index.
  • Then a character from alphaNumeric string is retrieved according to the random number stored in index. This character is stored in randomChar variable.
  • Lastly, this character in randomChar is appended to the string builder object sb.
  • When the loop breaks, all the appended characters are displayed on the screen.