随机密码生成

随机密码生成

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <time.h>

using namespace std;

const int PWDLENGTH = 10; //定义密码长度

int main() {
    
    ofstream codefile ("codefile.txt", ios_base::app);

    string password(PWDLENGTH,'0');

    srand((unsigned)time(NULL));

    for (int i = 0; i<9; i++)
    {
        // a 是 0-61 之间的随机数
        int a = rand()%62;

        if (a>=10 && a<=35) // A-Z
        {
            char c = a -10 +'A';
            password[i] = c;
        }
        else if ( a>=36 && a<=61) //  a-z
        {
            char c = a - 36 + 'a';
            password[i] = c;
        }
        else
            password[i]= a + '0';
    }

    codefile << password << endl;

    return 0;

}

你可能感兴趣的:(随机密码生成)