c语言:生成6位数字验证码,随机数种子用微秒级别

头文件:#include <sys/time..h>

函数原型:int  gettimeofday(struct timeval *tv, struct timezone *tz);

参数描述:

struct timeval {

    time_t  tv_sec;  /*seconds*/

    suseconds_t  tv_usec;   /*microseconds:微秒*/

};

struct timezone {

    int tz_minuteswest;  

    int tz_dsttime;   

};

生成6位数字验证码:

char * get_verification code(char *dest, int n)

{

    struct timeval tv;

    gettimeofday(&tv, NULL);

    srand(tv.tv_usec);

    memset(dest, 0, sizeof(dest));

    sprintf(dest, "%d", rand()%9+1);/*首位不能为0*/

    for (int i = 1; i <n; i++)

        sprintf(dest+strlen(dest), "%d", rand()%10);

    dest[i] = '\0';

    return dest;

}

猜你喜欢

转载自blog.csdn.net/weixin_42377147/article/details/89532084