play <- function(x){
x <- 2*(rbinom(1,1,0.5)) -1 +x;
x
}
game <- function(x){
while( x >0 && x <5){
x <- play(x);
}
x
}
y = replicate(1000, game(1))
backrupt=sum(y==0)/1000
win=sum(y==5)/1000
backrupt
## [1] 0.804
win
## [1] 0.196
The following version of the gambler’s ruin problem was Huygen’s formulation of a problem discussed in a correspondence between Pascal and Fermat: Each player starts with 12 points, and a successful roll of the three dice for a player (getting an 11 for the first player or a 14 for the second) adds one to that player’s score and subtracts one from the other player’s score; the loser of the game is the first to reach zero points. What is the probability of victory for each player? See A note on the history of the gambler ruin problem.
Of course you can compute the probabilities of getting \(11\) and \(14\), but you can electronically roll dice:
sample(6, 10, replace=TRUE)
## [1] 4 1 1 3 1 4 5 1 6 2
playdice <- function(x){
dice= sum(sample(6, 3, replace=TRUE))
if (dice==11){x <- x+1}
if (dice==14){x<-x-1}
x
}
gameH <- function(x){
while( x >0 && x <24){
x <- playdice(x);
}
x
}
y = replicate(1000, gameH(12))
backrupt=sum(y==0)/1000
win=sum(y==24)/1000
backrupt
## [1] 0.002
win
## [1] 0.998
threeplay <- function(x){
dice= sample(3, 1, replace=TRUE)
if (dice==1){x <-c(x[1]+2, x[2]-1, x[3]-1 )}
if (dice==2){x<-c(x[1]-1, x[2]+2, x[3]-1 )}
if (dice==3){x<-c(x[1]-1, x[2]-1, x[3]+2 )}
x
}
gametrack <- function(x){
n=0
while( x[1] >0 && x[2] >0 && x[3] >0){
x <- threeplay(x)
n <- n+1
}
n
}
y = replicate(10000, gametrack(c(5,5,5)))
mean(y)
## [1] 9.6269
For more information on the general case of \(n\) players see The asymmetric n-player gambler’s ruin problem with equal initial fortunes.
Version: 12 October 2020