-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug3new.c
36 lines (28 loc) · 821 Bytes
/
bug3new.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int die, lastdie, count, unfinished, throws;
char *dienames[] = {"ones", "twos", "threes", "fours", "fives", "sixes"};
srand(time(NULL));
printf("This program throws dice until we get the same number thrice in a row\n");
count = 0;
unfinished = 1;
lastdie = 0;
throws = 0;
while (unfinished){
/* Simulate a throw */
throws++;
die = rand()%6 + 1;
printf("Throw %d shows %d dots\n", throws, die);
/* Compare to the last one */
if (die == lastdie)
count++;
else
count = 0;
lastdie = die;
unfinished -= count == 2;
}
printf("We got three %s after %d throws\n", dienames[die-1], throws);
return 1;
}