码迷,mamicode.com
首页 > 其他好文 > 详细

C:产生随机数

时间:2020-01-29 01:18:46      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:printf   signed   turn   include   系统时间   srand   返回值   一个   数值   

函数说明

#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:产生随机数

标签:printf   signed   turn   include   系统时间   srand   返回值   一个   数值   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!