Java Program to Create random strings

Java Program to Create random strings

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data. Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this: String s = “Hello World”;

In this Article, we will write a program to Create random strings.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

Random Strings :

Random strings are sequences of characters generated randomly, without any specific pattern or meaning. They can consist of letters, numbers, and special characters, and are often used for various purposes, such as generating passwords, unique identifiers, or nonce values for cryptography.
In Java, you can create random strings using the java.util.Random class or the java.security.SecureRandom class.

Program to Create random strings :

Run
// Importing the random package
import java.util.Random;

public class Main{
    // All Characters for the random Number
    static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    // Initializing the random variable
    static Random random = new Random();

    public static String generateRandomString(int length){
        StringBuilder sb = new StringBuilder();
        
        // Generating Random String using loop
        for (int i = 0; i < length; i++) {
            sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args){
        // Initializing the length of the String
        int length = 10;
        
        // Creating Random String by calling generateRandomString method
        String randomString = generateRandomString(length);
        System.out.println("Random string of length " + length + ": " + randomString);
    }
}
Output:

Random string of length 10: VWJFABGPOL

Explanation :

The above program uses the java.util.Random class to generate a random string of the specified length. The generateRandomString method takes an argument for the desired length of the string and generates a string by repeatedly picking a random character from the CHARACTERS string and appending it to a StringBuilder object.

Program to Create random Alphanumeric Strings :

Run
// Importing the random package
import java.util.Random;

public class Main{
    // Creating a String of all lowercase and uppercase alphabeta and numbers
    static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    
    // Initializing an object of random class
    static Random random = new Random();

    public static String generateRandomString(int length){
        StringBuilder sb = new StringBuilder();
        
        // Generating String of Alphanumeric characters
        for (int i = 0; i < length; i++){
            sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args){
        // Initializing the length of the String
        int length = 10;
        
        // Calling the generateRandomString function
        String randomString = generateRandomString(length);
        System.out.println("Random alphanumeric string of length " + length + ": " + randomString);
    }
}
Output:

Random alphanumeric string of length 10: NjV8fB2N7a

In this program, a random alphanumeric string of the specified length is generated by using the java.util.Random class. The generateRandomString method takes a length as an argument and generates a random string of that length by repeatedly picking a random character from the CHARACTERS string, which consists of all uppercase and lowercase letters and numbers, and appending it to a StringBuilder object.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription