C++は1-10000の間の10000個の重複しない乱数を生成することを実現する
699 ワード
#include
#include
#include
#include
#include
using namespace std;
const int MaxN = 1e4 + 10;
int a[MaxN], b[MaxN];
int main() {
srand((unsigned)time(NULL));
for(int i = 0; i < 10000; ++i)
a[i] = i + 1;
int index, temp, rest = 10000;
for(int i = 0; i < 10000; ++i) {
index = rand() % rest;
b[i] = a[index];
temp = a[index];
a[index] = a[rest - 1];
a[rest - 1] = temp;
rest--;
}
for(int i = 0; i < 10000; ++i)
printf("%d ", b[i]);
printf("
");
return 0;
}