C/C++ 生成随机数

/*
	通过系统时间,生成真正的随机数
*/
#include<cstdio>
#include<time.h>
#include<stdlib.h>
int main(){
	int range = 100; // 指定随机数范围,本代码100以内
	const int number = 10; // 数组长度
	int arry[number];

	srand((int)time(0));// 使用系统时间生成真正随机数
	//srand()函数需要头文件stdlib.h
	//time()函数需要头文件time.h

	for(int i = 0; i < number; i++){
		arry[i] = rand()%range; //生成随机数

	}
	for(int i = 0; i < number; i++){
		printf("%d ",arry[i]);
	}
	printf("\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/i_CodeBoy/article/details/91409825