路漫修远

 

2006年10月27日

随机密码生成

#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;

}

posted @ 2006-10-27 09:35 johnniac 阅读(259) | 评论 (0)编辑 收藏

仅列出标题  

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论