Skip to content

Instantly share code, notes, and snippets.

@duarteguilherme
Last active March 14, 2016 21:14
Show Gist options
  • Save duarteguilherme/99944deff622526ee01d to your computer and use it in GitHub Desktop.
Save duarteguilherme/99944deff622526ee01d to your computer and use it in GitHub Desktop.
return_tosses <- function(value1, value2) {
t1 <- rbinom(1,1,.5)
i <- 1
while ( TRUE ) {
t2 <- rbinom(1,1,.5)
i <- i + 1
if ( t1 == value1 & t2 == value2)
return(i)
t1 <- t2
}
}
N <- 100000
alice <- vector(mode="integer",length=N)
bob <- vector(mode="integer",length=N)
# 1 = head, 0 = tail
for (i in 1:N) {
alice[i] <- return_tosses(1,0)
bob[i] <- return_tosses(1,1)
}
mean(alice)
## [1] 3.99914
mean(bob)
## [1] 5.97424
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment