天天看點

随機生成1000w内不重複的随機數

http://blog.csdn.net/v_july_v/article/details/6630757

從上面部落格中擷取的算法,編譯測試通過

void testMake1000WRand()

{

const int size = 10000000;

int *num = new int [size];

int n;

FILE *fp = fopen("F:\\rasterTest\\data.txt", "w");

assert(fp);

for (n = 1; n <= size; n++)

num[n-1] = n;

srand((unsigned)time(NULL));

int i, j;

for (n = 0; n < size; n++)

{

i = (rand() * RAND_MAX + rand()) % 10000000;

j = (rand() * RAND_MAX + rand()) % 10000000;

swap(num[i], num[j]);

}

for (n = 0; n < size; n++)

fprintf(fp, "%d\t", num[n]);

fclose(fp);

delete num;

return ;

}

繼續閱讀