-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRolling a Six-Sided Die 6,000,000 Times.c
59 lines (52 loc) · 1.74 KB
/
Rolling a Six-Sided Die 6,000,000 Times.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // include for time()
// function main begins program execution
int main(void)
{
unsigned int frequency1 = 0; // rolled 1 counter
unsigned int frequency2 = 0; // rolled 2 counter
unsigned int frequency3 = 0; // rolled 3 counter
unsigned int frequency4 = 0; // rolled 4 counter
unsigned int frequency5 = 0; // rolled 5 counter
unsigned int frequency6 = 0; // rolled 6 counter
unsigned int roll; // roll counter, value 1 to 6000000
int face; // represents one roll of the die, value 1 to 6
srand(time(NULL)); // seed the random number generator
// loop 6000000 times and summarize results
for (roll = 1; roll <= 6000000; ++roll)
{
face = 1 + rand() % 6; // random number from 1 to 6
// determine face value and increment appropriate counter
switch (face)
{
case 1: // rolled 1
++frequency1;
break;
case 2: // rolled 2
++frequency2;
break;
case 3: // rolled 3
++frequency3;
break;
case 4: // rolled 4
++frequency4;
break;
case 5: // rolled 5
++frequency5;
break;
case 6: // rolled 6
++frequency6;
break; // optional
} // end switch
} // end for
// display results in tabular format
printf("%s%13s\n", "Face", "Frequency");
printf("1%13u\n", frequency1);
printf("2%13u\n", frequency2);
printf("3%13u\n", frequency3);
printf("4%13u\n", frequency4);
printf("5%13u\n", frequency5);
printf("6%13u\n", frequency6);
return 0; // indicate successful termination
} // end main