Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to replicate results multiple times

I have a sim here as a fun project that has a loaded coin flip and the first person to get 15 heads or tails respectively wins. I am trying to replicate it so the game is played x number of times and the score results and win results are stored. Here is the code I have written that functions as a one time run, but I can't seem to make it replayable, any help appreciated:

MeScore256<-0
OppScore256<-0
turns<-0
Fence <- c("Me", "Opps")  #Names

while(MeScore256<15 & OppScore256<15){
  flip<-sample(c(0,1), size = 1, replace = TRUE, prob = c(0.4,0.6))
  if (flip==1){
    MeScore256<-MeScore256+1
  }else{
    OppScore256<-OppScore256+1
  }
  turns<-turns+1
  }
print(MeScore256)
print(OppScore256)

if (MeScore256==15){print ("you win round 256")
   
} 

if (OppScore256==15){print("Opps win 256 :(")}
  

Tried using "replicate" function, keeps failing, I think it has something to do with the way the score counter works and the score carries over from last round, but I'm not 100% sure.

like image 560
Oscar Scott Avatar asked Oct 21 '25 11:10

Oscar Scott


2 Answers

An approach with base R:

## initalize score list for players "Me" and "Opps"
scores <- list(Me = 0, Opps = 0)

recursive function simulating a single game:

gamble <- function(scores, top_score = 0, score_limit = 15, turns = 1){
    flip <- sample(1:2, size = 1, prob = c(0.4,0.6))
    scores[[flip]]  <- scores[[flip]] + 1
    top_score <- Reduce(scores, f = max)
    if(top_score < score_limit){
        turns  <- turns + 1
        gamble(scores, top_score, score_limit, turns)
    } else {
        c(scores, turns = turns)
    }
}

play 5 games and return scores and winning turns per game as dataframe:

do.call(rbind, Map(1:5, f = \(i) gamble(scores))) |>
    as.data.frame()
## +   Me Opps turns
## 1  8   15    23
## 2  8   15    23
## 3 15    9    24
## 4  5   15    20
## 5 14   15    29
like image 140
I_O Avatar answered Oct 23 '25 02:10

I_O


You could have a function store the result of an iteration in a tibble, then use map to run, say, 10 iterations and bind_rows() to get a single set of results:

library(tidyverse)

MeScore256 <- 0
OppScore256 <- 0
turns <- 0
Fence <- c("Me", "Opps") # Names

run_sim <- \(MeScore256 = 0, OppScore256 = 0, turns = 0){
  while (MeScore256 < 15 & OppScore256 < 15) {
    flip <- sample(c(0, 1), size = 1, replace = TRUE, prob = c(0.4, 0.6))
    if (flip == 1) {
      MeScore256 <- MeScore256 + 1
    } else {
      OppScore256 <- OppScore256 + 1
    }
    turns <- turns + 1
  }
  # print(MeScore256)
  # print(OppScore256)

  if (MeScore256 == 15) {
    # print("you win round 256")
    who_wins <- "you win round 256"
  }

  if (OppScore256 == 15) {
    # print("Opps win 256 :(")
    who_wins <- "Opps win 256 :("
  }
  
  tibble(MeScore256, OppScore256, who_wins)
}

set.seed(2)
map(1:10, run_sim) |> 
  bind_rows()
#> # A tibble: 10 × 3
#>    MeScore256 OppScore256 who_wins         
#>         <dbl>       <dbl> <chr>            
#>  1         15           8 you win round 256
#>  2         15           8 you win round 256
#>  3         10          15 Opps win 256 :(  
#>  4         15           1 you win round 256
#>  5         15           6 you win round 256
#>  6         15           0 you win round 256
#>  7         15           4 you win round 256
#>  8         15           7 you win round 256
#>  9         15           5 you win round 256
#> 10         15           1 you win round 256

Created on 2024-04-03 with reprex v2.1.0

like image 36
Carl Avatar answered Oct 23 '25 01:10

Carl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!