【编程珠玑】生成k个1-n的随机数

/*==========================================================*\
| gen $1 distinct integers from U[0,$2)
\*==========================================================*/
#define MAXN 2000000
int x[MAXN];
int randint(int a, int b)
{
	return a + (RAND_MAX * rand() + rand()) % (b + 1 - a);
}

int main(int argc, char *argv[])
{	int i, k, n, t, p;
	srand((unsigned)time(NULL));
	k = atoi(argv[1]);
	n = atoi(argv[2]);
	for (i = 0; i < n; i++)
		x[i] = i;
	for (i = 0; i < k; i++) {
		p = randint(i, n-1);
		swap(x[i], x[p]);
		printf("%d\n", x[i]);
	}
	return 0;
}

你可能感兴趣的:(编程,null)