无序-有重复数据产生
在C/C++中,rand函数可以产生[0, 32767]之间的伪随机整数,且服从均匀分布。但是有的时候为了数据测试的需求,我们需要产生无序但是有重复的大范围数据,具体的产生方法如下,代码编译环境为DEV C++。
#include <bits/stdc++.h>
using namespace std;
#define COUNT 10000 //元素个数
int main(){
int i=0,j,x;
int a[COUNT];
clock_t start, finish;
double run_time;
start=clock();
ofstream outfile("C:\\AT\\disorderWithoutRepetition10000.txt"); //写入文件
srand(time(0));
while (i<COUNT){
x=rand();
for (j=0;j<i;j++){
if (x==a[j]) break;
}
if (j>=i){
a[i++]=x; //不重复,加入数组
}
}
for (i=0;i<COUNT;i++){
outfile<<a[i]<<" ";//空格是为了避免数值连接在一起加上去的
}
finish=clock();
run_time=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"It took "<<run_time<<" seconds!";
return 0;
}
运行结果:
记事本文件数据:
有序-无重复数据产生
有序但无重复的数据产生相较简单,只需要分段有序产生即可,具体代码如下所示:
#include <bits/stdc++.h>
using namespace std;
#define NUMBER 30 //间隔30产生元素
#define COUNT 10000 //元素个数
int main(){
int x;
ofstream outfile("C:\\AT\\orderWithoutRepetition10000.txt"); //写入文件
srand(time(0));
for(int i=0;i<COUNT;i++){
x=NUMBER*i+rand()%NUMBER;
outfile<<x<<" "; //空格是为了避免数值连接在一起加上去的
}
cout<<"Ok.Press any key!";
return 0;
}
运行结果:
记事本文件数据: