Since there's no way the OP would try to submit this has his own program, I figured I'd post it. I just whipped it up for fun:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void roll(int *dice, unsigned char num)
{
dice[0] = ((num & 0x7) % 6) + 1;
dice[1] = (((num >> 3) & 0x7) %6) + 1;
}
void roll_dice_urandom(int *dice, FILE *fp)
{
int num;
if((num = fgetc(fp)) == EOF)
{
puts("Error reading from urandom!");
exit(EXIT_FAILURE);
}
roll(dice, (unsigned char)num);
}
void roll_dice_rand(int *dice, FILE *fp)
{
roll(dice, (unsigned char)rand());
}
void show_bar(int len)
{
while(len--)
putchar('X');
putchar('\n');
}
int main(int argc, char **argv)
{
int dice[2];
int *sums = NULL;
int nrolls;
int i;
FILE *fp = NULL;
void (*roll_dice)(int *, FILE *);
int frequency[11] = { 0 };
if(argc < 2 || argc > 3)
{
printf("Usage: %s <number of rolls> [r]\n", argv[0]);
puts("The program tries to use /dev/urandom for random numbers."
" If it can't, it will fall back on rand(). Specifying 'r'"
" on the command lind overrides normal behavior and forces"
" the use of rand() even if /dev/urandom is available.");
exit(EXIT_FAILURE);
}
if((nrolls = atoi(argv[1])) < 1)
{
puts("Number of rolls must be greater than 0");
exit(EXIT_FAILURE);
}
if(argc == 3)
{
if(strcmp(argv[2], "r"))
{
puts("Invalid command line option.");
exit(EXIT_FAILURE);
}
srand(time(NULL));
roll_dice = roll_dice_rand;
}
else
{
if(!(fp = fopen("/dev/urandom", "r")))
{
srand(time(NULL));
roll_dice = roll_dice_rand;
}
else
roll_dice = roll_dice_urandom;
}
if(!(sums = malloc(sizeof(int) * nrolls)))
{
puts("Memory allocation error!");
exit(EXIT_FAILURE);
}
for(i = 0;i < nrolls;++i)
{
roll_dice(dice, fp);
sums[i] = dice[0] + dice[1];
}
if(fp)
fclose(fp);
for(i = 0;i < nrolls;++i)
frequency[sums[i] - 2]++;
printf("Sums%s:\n", nrolls > 50 ? " (first 50)" : "");
for(i = 0;i < (nrolls > 50 ? 50 : nrolls);++i)
printf("%d ", sums[i]);
putchar('\n');
puts("\nFrequency:");
for(i = 0;i < 11;++i)
printf("%d:%d ", i + 2, frequency[i]);
putchar('\n');
for(i = 0;i < 11;++i)
{
printf("%2d ", i + 2);
show_bar((int)(((double)frequency[i] / nrolls) * 75));
}
free(sums);
return EXIT_SUCCESS;
}
Code:
itsme@dreams:~/C$ ./dice 36000
Sums (first 50):
7 8 6 6 8 10 8 11 2 6 6 3 11 5 6 5 5 3 4 8 6 8 6 6 3 7 5 9 8 4 5 7 5 7 6 9 6 11 6 6 6 4 9 6 6 9 9 8 2 11
Frequency:
2:2342 3:4418 4:4403 5:4438 6:5149 7:5693 8:3882 9:2324 10:1669 11:1130 12:552
2 XXXX
3 XXXXXXXXX
4 XXXXXXXXX
5 XXXXXXXXX
6 XXXXXXXXXX
7 XXXXXXXXXXX
8 XXXXXXXX
9 XXXX
10 XXX
11 XX
12 X
itsme@dreams:~/C$
EDIT: My roll() function might be a really poor way to get proper randomness, because those results look suspicious to me.
EDIT 2: At least I'm getting suspicious results consistently! I ran the program with the same parameters at least a dozen times and the graph at the end looks identical every time, the sum frequencies are so close to the same on every run.