# 🔐 Random Password Generator
This repository contains **two versions** of a simple password generator — one written in **Python** and another in **C++**.
Both programs generate strong random passwords using a mix of letters, digits, and special characters.
---
## 🧩 Project Structure
Project structure:
password-generator/
│
├── password_generator.py # Python version
├── password_generator.cpp # C++ version
└── README.md # Project documentation
---
## 💻 Python Version
### 🧠 How It Works
1. Imports the `random` module.
2. Defines a string `chars` with lowercase, uppercase, digits, and special symbols.
3. Asks the user to input the desired password length.
4. Randomly selects characters and builds a password.
5. Displays the generated password.
### 🧾 Code
```python
import random
chars = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+-'
length = int(input("Enter length: "))
password = ""
for a in range(length):
password += random.choice(chars)
print(password)
-
Ensure Python 3 is installed.
-
Save the code as
password_generator.py. -
Run in terminal:
python password_generator.py
-
Enter your desired password length.
-
The generated password will be printed.
Enter length: 10
Generated password: aB8!zQ1#Lp
- Uses
rand()andsrand(time(0))to generate random numbers. - Combines uppercase, lowercase, digits, and special characters.
- Randomly selects characters from this set to form a password.
- Prints the result to the console.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string generatePassword(int length) {
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lower = "abcdefghijklmnopqrstuvwxyz";
string digits = "0123456789";
string special = "!@#$%^&*()-_=+[]{};:,.<>?";
// Combine all character sets
string allChars = upper + lower + digits + special;
string password = "";
// Seed the random number generator
srand(time(0));
for (int i = 0; i < length; i++) {
int index = rand() % allChars.length();
password += allChars[index];
}
return password;
}
int main() {
int length;
cout << "Enter the desired password length: ";
cin >> length;
string password = generatePassword(length);
cout << "Generated Password: " << password << endl;
return 0;
}-
Save the file as
password_generator.cpp. -
Compile and run using:
g++ password_generator.cpp -o password_generator ./password_generator
-
Enter your desired password length.
-
The program will print a random password.
Enter the desired password length: 12
Generated Password: Zs#8yP@cW2fG
- Use 12+ character passwords for stronger security.
- You can modify the character sets to include or exclude certain symbols.
- For enhanced security, the Python version can use the
secretsmodule instead ofrandom.
- Random number generation (
random/rand) - String manipulation
- Loops and input handling
- Cross-language implementation (Python & C++)