您的位置:首页 > 运维架构 > Linux

linux c 生成八位的随机密码

2015-09-22 21:06 796 查看
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

char  pool[] =  {
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z','A','B','C','D',
'E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X',
'Y','Z'
};  //随机池

int main(){
// srand(time(0));
/*
使用 time(0) 做种子,在一秒之内种子是不会变的
通过使用微妙,来增加随机数的随机性
*/
struct timeval tpstart;
gettimeofday(&tpstart,NULL);
srand(tpstart.tv_usec);
char pwd[9];
pwd[8] = '\0';
int i = 0;
while(i != 8){
pwd[i++] = pool[rand()%sizeof(pool)];
}

printf("%s\n",pwd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: