阅读背景:

C-产生0-9,x随机数

来源:互联网 
 
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define S 11

static int a[S];
static int maxn = S – 1;


void unique_rand_init() {
	for (int i = 0; i < S; i++)
		a[i] = i;
}


int unique_rand() {
	int ran = rand() % (maxn + 1);
	int r = a[ran];
	int tmp = a[maxn];
	a[maxn] = a[ran];
	a[ran] = tmp;
	if (–maxn < 0)
		maxn = S – 1;
	return r;
}


int main() {
	srand(time(NULL));
	unique_rand_init();
	for (int i = 0; i < 11; i++)
		printf(“%d \n”, unique_rand());
	system(“pause”);
}#include <stdlib.h>
#include <stdio.h>
#incl



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: