系统生成随机数

在之前的猜数字举例练习中需要用到系统生成随机数的程序

这时候就需要我们添加随机数种子,用来利用当前系统时间生成随机数,防止每次随机数都一样

首先需要添加time时间头文件如下:

#include<ctime>

然后在执行程序中加入

srand((unsigned int)time(NULL));

rand()%数字

如:

srand((unsigned int)time(NULL));
rand()%100     

表示生成0~99随机数

posted @ 2022-09-10 13:40  书文阁下  阅读(91)  评论(0)    收藏  举报