[ create a new paste ] login | about

Link: http://codepad.org/0qxox1if    [ raw code | output | fork ]

C, pasted on Oct 1:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DICE 6

int nrandom(int n)
{
	return rand() % n;
//	return n * rand() / (RAND_MAX + 1);
}

int main()
{
	int hist[DICE] = {0};
	int i, dice;

	printf("RAND_MAX=%d\n", RAND_MAX);
	srand((unsigned)time(NULL));
	for (i = 0; i < 10000; i++) {
		dice = nrandom(DICE);
		if (dice < 0 || DICE <= dice) {
			fprintf(stderr, "error: dice=%d\n", dice);
			return 1;
		}
		hist[dice]++;
	}
	for (i = 0; i < DICE; i++) {
		printf("%d=%d\n", i, hist[i]);
	}
	return 0;
}


Output:
1
2
3
4
5
6
7
RAND_MAX=2147483647
0=1702
1=1614
2=1665
3=1654
4=1646
5=1719


Create a new paste based on this one


Comments: