-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroke.cpp
More file actions
105 lines (93 loc) · 2.9 KB
/
broke.cpp
File metadata and controls
105 lines (93 loc) · 2.9 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
int main()
{
int init[3]; /* Initial number of quarters */
int person[3]; /* Current number of quarters for each person */
double p; /* Fairness of the coin (probability of the coin flip) */
double r; /* Value to be used for random probability between 0 and 1 */
double totalflips; /* Total flips over all runs */
/*====================================================*/
/* Declare other variables here */
/*====================================================*/
int sequenceflips;
int flip[3];
int flip_sum;
int i, sequences;
totalflips = 0;
/*====================================================*/
/* Seed the random number generator */
/*====================================================*/
printf("RAND_MAX is %u\n", RAND_MAX);
srand(1000);
/*====================================================*/
/* I/O for requesting initial values and fairness of */
/* the coin init array and p should be initialized */
/* here by the user */
/*====================================================*/
printf("Enter the initial number of coins for reach person seperated by spaces: ");
scanf("%i %i %i", &init[0], &init[1], &init[2]);
printf("Enter the probability (p) of the coin flips: ");
scanf("%lf", &p);
/*====================================================*/
/* Monte Carlo simulation */
/* Should loop 100,000 times */
/* At this point you should be able to use the */
/* same algorithm that your Matlab code did */
/*====================================================*/
for(sequences=0; sequences < 100000; sequences++)
{
sequenceflips = 0;
person[0] = init[0];
person[1] = init[1];
person[2] = init[2];
while(person[0] > 0 && person[1] > 0 && person[2] > 0)
{
for(i = 0; i<3; i++)
{
r = ((double)rand()/((double)RAND_MAX + (double)1));
if(r<p)
flip[i] = 0;
else
flip[i] = 1;
}
flip_sum = flip[0] + flip[1] + flip[2];
if(flip_sum == 1 || flip_sum == 2)
{
if(flip_sum == 1)
{
for(i = 0; i<3; i++)
{
if(flip[i] == 0)
flip[i] = -1;
else
flip[i] = 2;
}
}
else
{
for(i=0;i<3;i++)
{
if(flip[i] == 0)
flip[i] = 2;
else
flip[i] = -1;
}
}
for(i=0;i<3;i++)
{
person[i] = person[i] + flip[i];
}
}
sequenceflips = sequenceflips + 1;
}
totalflips = totalflips + sequenceflips;
}
/*====================================================*/
/* Calculate the total flips and print out that value */
/* This code is complete and should not need to be */
/* modified. */
/*====================================================*/
totalflips = totalflips / 100000;
printf("Average flips for 100,000 runs: %f\n",totalflips);
}