【技巧】C/C++生成随机数数组(rand(),srand(),time()函数介绍)
关于rand(),srand()生成随机数原理,本人了解不深,目前仅仅是会简单使用的水平。
基于基本的生成随机数方法,通过一些策略调整所需随机数的范围
#include#include #include #include using namespace std; void Random(int *a,int n,int l,int r)//最大范围:0~32767 { srand(time(0)); for(int i=0; i 生成n个随机数 根据不同算法题实际需要更改
#include#include #include #include using namespace std; int Random(int n)//最大范围:-32767~32767,实际范围 -(n-1)~(n-1),根据需求挑战范围 { int tmp; tmp = rand() % n; if(rand()%2) tmp = -tmp; //注意,这里的rand()和上一行的不一样! return tmp; } int main() { #ifndef ONLINE_JUDGE //本地运行的时候执行该块语句 //freopen("in.txt", "r", stdin); //读入文件"in.txt"中的数据,本题用不到 freopen("out.txt", "w", stdout); //输出数据到同目录文件"out.txt"中 #endif int N=200; //数组元素的个数,即生成随机数的个数 cout<



