-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1p9.R
65 lines (60 loc) · 1.81 KB
/
a1p9.R
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
num_of_experiments <- 1000
play_a_round <- function(PaulsMoney, LindasMoney) {
number_of_games <- 0
while (PaulsMoney > 0 && LindasMoney > 0) {
LindaWinsGame <- sample(c(TRUE,FALSE), size = 1)
if (LindaWinsGame) {
PaulsMoney <- PaulsMoney - 2
LindasMoney <- LindasMoney + 2
} else {
PaulsMoney <- PaulsMoney + 2
LindasMoney <- LindasMoney - 2
}
number_of_games <- number_of_games + 1;
}
return(list(PaulWinsRound = (LindasMoney == 0), number_of_games))
}
run_simulation <- function(PaulsMoney, LindasMoney) {
PaulWinCount <- 0
number_of_games_sum <- 0
for (count in 1:num_of_experiments) {
results <- play_a_round(PaulsMoney, LindasMoney)
PaulWinsRound <- results[[1]][1]
number_of_games <- results[[2]][1]
if (PaulWinsRound == TRUE) {
PaulWinCount <- PaulWinCount + 1
}
number_of_games_sum <- number_of_games_sum + number_of_games
}
print('The probability that Paul ends up ruined: ')
print(1 - PaulWinCount / num_of_experiments)
print('The expected number of played games: ')
print(number_of_games_sum * 1.0 / num_of_experiments)
}
run_simulation(50, 50)
run_simulation(50, 500)
run_simulation(50, 5000)
run_simulation(50, 5000000)
# > run_simulation(50, 50)
# [1] "The probability that Paul ends up ruined: "
# [1] 0.505
# [1] "The expected number of played games: "
# [1] 617.05
#
# > run_simulation(500, 50)
# [1] "The probability that Paul ends up ruined: "
# [1] 0.911
# [1] "The expected number of played games: "
# [1] 6143.433
#
# > run_simulation(5000, 50)
# [1] "The probability that Paul ends up ruined: "
# [1] 0.991
# [1] "The expected number of played games: "
# [1] 53253.5
#
# > run_simulation(5000000, 50)
# [1] "The probability that Paul ends up ruined: "
# [1] 1
# [1] "The expected number of played games: "
# [1] 821708