随机数(random)

时间:2023-03-09 03:10:05
随机数(random)

在测试你的程序是否超时时,可以随机生成一组大数据,进行一下测试。

当然如果你考场上一道题直接读不懂不会做的时候,可以random一下,拼一下RP嘛。2333.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
freopen("xx.in","w",stdout);
srand((unsigned)time(NULL));
for(int i=;i<;i++)//随机生成1000个数
cout<<rand()%+<<endl;//如果这些数有大小限制,可以对其随机生成的数据取模
fclose(stdout);
return ;
}

随机生成

注意:random()函数只能生成[0,0x7fff]范围内的数.(0x7fff为32767).如果想要生成比起范围更大的数,可以随机生成一些数再乘以10^k使其变为更大的数。

#include<ctime>
#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[];
int main()
{
freopen("xx.in","w",stdout);
srand((unsigned)time(NULL));
for(int i=;i<;i++)
a[i]=i+;
random_shuffle(a,a+);//打乱数组中数的顺序,不改变值
for(int i=;i<;i++)
cout<<a[i]<<endl;
fclose(stdout);
return ;
}

打乱顺序