首页 > 其他 > 详细

C:产生随机数

时间:2020-01-29 00:22:12      阅读:101      评论:0      收藏:0      [点我收藏+]

函数说明

#include <time.h>
time_t time(time_t *t);
功能:获取当前系统时间
参数:常设置为NULL
返回值:当前系统时间, time_t 相当于long类型,单位为毫秒

#include <stdlib.h>
void srand(unsigned int seed);
功能:用来设置rand()产生随机数时的随机种子
参数:如果每次seed相等,rand()产生随机数相等
返回值:无

#include <stdlib.h>
int rand(void);
功能:返回一个随机数值
参数:无
返回值:随机数

使用举例

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    time_t tm = time(NULL);//得到系统时间
    srand((unsigned int)tm);//随机种子只需要设置一次即可

    int r = rand();
    printf("r = %d\n", r);

    return 0;
}

C:产生随机数

原文:https://www.cnblogs.com/wbyixx/p/12239376.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!