Kodekraftt
/Blog
/Random Password Generator In Java Rules Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Random Password Generator in Java — Rules, Examples and Program

Learn how to generate a secure random password in Java using uppercase letters, lowercase letters, digits, and special characters with customizable length.

Random Password Generator in Java

A random password generator is commonly used in real-world applications such as account creation, password reset systems, and security tools.

In this program, we generate a strong password by combining uppercase letters, lowercase letters, digits, and special characters.

Password Generation Rules

The generated password will:

  • Contain uppercase letters (A–Z)
  • Contain lowercase letters (a–z)
  • Contain digits (0–9)
  • Contain special characters
  • Have a user-defined length

Example

Input: Password Length = 10

Output: A9@kP3#xT2

Logic Explanation

  1. Create a string containing all allowed characters.
  2. Use Java’s Random class to generate random indexes.
  3. Select characters randomly from the allowed set.
  4. Repeat until the desired password length is reached.

Java Program to Generate Random Password


import java.util.Random;
import java.util.Scanner;

public class RandomPasswordGenerator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter password length: ");
        int length = sc.nextInt();

        String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lower = "abcdefghijklmnopqrstuvwxyz";
        String digits = "0123456789";
        String special = "!@#$%^&*()_+-={}[]|:;<>,.?/";

        String allChars = upper + lower + digits + special;

        Random random = new Random();
        String password = "";

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(allChars.length());
            password = password + allChars.charAt(index);
        }

        System.out.println("Generated Password: " + password);
    }
}

Sample Output


Enter password length: 12
Generated Password: A@9kL3#xP7!Q

Important Notes

  • Password strength increases with length.
  • Each run generates a different password.
  • Characters may repeat.

Practice Challenges

  1. Ensure at least one uppercase, lowercase, digit, and special character.
  2. Generate password without repeating characters.
  3. Allow user to choose which character sets to include.
  4. Generate multiple passwords at once.

Random password generation is a practical application of strings, random numbers, and security concepts in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.