随机数(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=0;i<1000;i++)//随机生成1000个数 
        cout<<rand()%100+1<<endl;//如果这些数有大小限制,可以对其随机生成的数据取模 
    fclose(stdout);
    return 0;
}
随机生成

 

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

 

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

 

posted @ 2016-11-03 10:34  外婆桥  阅读(286)  评论(0编辑  收藏  举报