#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gen_random_numbers(int *array, int len, int min, int max){
for (int i = 0; i < len; i++)
array[i] = rand() % (max - min + 1) + min;
}
int main() {
system("cls");
srand(time(0));
int numbers[100] = {};
gen_random_numbers(numbers, 100, 50, 70);
// create 100 arrays, each of them is in range of [50, 70]
return 0;
}
4条答案
按热度按时间ndh0cuux1#
创建阵列:
为随机数生成器设定种子
循环遍历数组并填充它!:
这是一个开始,但是兰德()的取值范围比你想要的随机数取值范围要大得多,有很多方法可以缩小取值范围,如果你不在乎随机数是否完全随机,你可以使用modulo运算符,其中
13 % 10 = 3
。这是为
int
s准备的,我想给读者留下一些乐趣。gojuced72#
如果数字在50和70之间,那么我会说,尝试modulo和c的兰德()函数。所以首先,因为你想使用随机数,我建议包括标准库。
I模20的原因是因为50和70的差是20,所以,如果你假设50是零,那么70将是20,所以我们将产生的任何数字都将在这些数字之间。希望它能有所帮助!*/
uidvcgyl3#
你可以用这个来定义
rand
函数的范围:brc7rcf04#