Generate Random Password In C++

What we are going to see through this article is we will see in detail Generate Random Password In C++. Passwords are important for cyber security.

A good password should be at least 8 random characters long and have lowercase letters, uppercase letters, numbers, and at least one special character.

The random function gives us a random value generated by the system. The system generates different values ​​for each run. Random values ​​vary from machine to machine, which means we can create as many values ​​as we want.

Generate Random Password In C++

Generate Random Password in C++

#include <iostream>
using namespace std;
const char alphanum[] = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int string_length = sizeof(alphanum)-1;
int main()
    int n;
    cout<<"Enter the length of password:";
    cout<<"Generated password:";
    for(int i = 0; i < n; i++)
    cout << alphanum[rand() % string_length];
    return 0;

We call that constant at random to generate random passwords using random functions. We have used the cstdlib library for random operation. A few sample programs with different user-defined values.

Read Also: How To Print An Array In Python


Enter the length of password:8
Generated password:i^FjD&9%
Enter the length of password:7
Generated password:xne8rWm
Enter the length of password:12
Generated password:nDzq!qf$tqV$
Enter the length of password:4
Generated password:*d0@

Final Words

What we learned through this article is to Generate Random Password In C++. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.

Leave a Comment