Mikael
Mikael

Reputation: 21

Trying to figure out a casino loop in R

My question is to create a code which makes me able to figure out the chance of winning the casino.

Here is the situation to simulate : There is a person named "Kim" who goes to the casino with the capital of 1 and Kim plays until she reaches 0 capital and then goes home or she wins 5 and then decides to go home. The problem I've reached is that I just make it so she bets 1 each time and wins 1. I would like to try make it that if Kim got capital 2 she will play for the 2 to win 4 or go home with 0. Which makes me come to the second problem. When Kim reaches 3 or 4 in capital she will only bet 2 or 1 depending on which she is on to reach capital 5 which counts as "Win". I want to simulate 1000 stimulations and see how many times Kim wins and how many times the casino wins.

So far my code looks like this: cow = (Chance of winning)

gamble <- function(capital, upper=5, lower=0, cow=0.5){
    while(capital < upper & capital>lower){
        if(runif(1)>cow){
       capital <- capital +1
         } else capital <- capital -1
    }
    capital
}


set.seed(1)
simulations <- 1000 
results <- c(1:simulations)
for(i in seq_along(results)){
  results[i] <- gamble(1)
}

 table(results)

Upvotes: 1

Views: 111

Answers (1)

user6278894
user6278894

Reputation:

Would something like this work?

gamble <- function(capital, upper=5, lower=0, cow=0.5){
  while(capital < upper & capital>lower){
    betsize = ifelse(capital %in% c(2,3),2,1)
    if(runif(1)>cow){
      capital <- capital + betsize
    } else capital <- capital - betsize
  }
  return(capital)
}

Or as Nicola suggested in the comments using the capital value:

gamble <- function(capital, upper=5, lower=0, cow=0.5){
      while(capital < upper & capital>lower){
        betsize<-min(capital,upper-capital)
        if(runif(1)>cow){
          capital <- capital + betsize
        } else capital <- capital - betsize
      }
      return(capital)
    }

From what I understood, if the capital is 1 she can bet only 1, if it is 2 or 3 she will play 2 and if it is 4 she will play 1.

Upvotes: 1

Related Questions