#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
/*
* 随机数的两个弊端:
* 1、种子不变,随机数结果是固定的
* 2、随机数的范围,默认范围:0~32767
*
* 生成任意范围之内的随机数
* 1、把这个范围变成包头不包尾,包左不包右的
* 2、拿尾巴减去开头,将得到的结果对随机数取余,并加上范围的开始
* */
// 设置种子
// 种子:不能固定不变,否则结果不变
// 用一个变化的数据去充当种子——时间
srand(time(NULL));
printf("1~100 7~23 8~49 12~87 17~39\n");
// 获取10个随机数
for (int i = 0; i < 10; i++) {
// 获取1-100以内的随机数:100 + 1 - 1 = 100, rand() % 100 + 1
int num1 = rand() % 100 + 1;
// 获取7-23以内的随机数:23 + 1 - 7 = 17, rand() % 17 + 7
int num2 = rand() % 17 + 7;
// 获取8-49以内的随机数:49 + 1 - 8 = 42, rand() % 42 + 8
int num3 = rand() % 42 + 8;
// 获取12~87以内的随机数:87 + 1 - 12 = 76, rand() % 76 + 12
int num4 = rand() % 76 + 12;
// 获取17~39(不包含39)以内的随机数:39 - 17 = 22, rand() % 22 + 17
int num5 = rand() % 22 + 17;
printf("%5d %4d %4d %5d %5d\n", num1, num2, num3, num4, num5);
}
return 0;
}