Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to generate a random password in C++

3 Answers

0 votes
#include <iostream>
#include <string>
#include <ctime>

std::string generatePassword(int password_length) {
    const std::string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$()";
    std::string password;
    int charset_len = charset.length();
    
    std::srand(std::time(0)); // Seed for random number generation

    for (int i = 0; i < password_length; i++) {
        password += charset.at(std::rand() % charset_len);
    }
    
    return password;
}

int main() {
    std::cout << generatePassword(10) << std::endl;
}

 
 
/*
run:
 
Aj)KiGrn(F

*/

 



answered Dec 22, 2024 by avibootz
0 votes
#include <iostream>
#include <ctime>

std::string generatePassword(int password_length) {
    std::srand(std::time(nullptr));
    std::string password(password_length, '*'); 
    
    for (int i = 0; i < password_length; i++) {
        password[i] = std::rand() % (126 - 33 + 1) + 33;
    }
    
    return password;
}

int main() {
    std::string password = generatePassword(10);

    std::cout << password << std::endl;
}


 
 
/*
run:
 
X6GM;s7kAu

*/

 



answered Dec 22, 2024 by avibootz
0 votes
#include <iostream>
#include <ctime>

std::string generatePassword(int password_length) {
    std::srand(std::time(nullptr));
    std::string password = "";
    
    for (int i = 0; i < password_length; i++) {
        password += std::rand() % (126 - 33 + 1) + 33;
    }
    
    return password;
}

int main() {
    std::string password = generatePassword(10);

    std::cout << password << std::endl;
}


 
 
/*
run:
 
g%wn\b|-W4

*/

 



answered Dec 22, 2024 by avibootz

Related questions

2 answers 93 views
93 views asked Dec 21, 2024 by avibootz
1 answer 84 views
1 answer 85 views
1 answer 69 views
1 answer 71 views
1 answer 72 views
72 views asked Dec 21, 2024 by avibootz
1 answer 71 views
...