Class Activity

Instruction: Work with a neighbor to answer the following questions. Solutions will be posted on the course website. To get started, download the class activity template file.

Roulette revisited

Previously, we consider the following gambling scenario:

Here is an R simulation to estimate the probability:

set.seed(279)

nsim <- 1000
results <- rep(0, nsim)
wheel <- c(rep("green", 2), rep("black", 18), rep("red", 18))

for(i in 1:nsim){
  money <- 50 # starting money

  while(money > 0 && money < 100){
    spin <- sample(wheel, size = 1)
    if(spin == "red"){
      money <- money + 1
    } else {
      money <- money - 1
    }
  }
  
  results[i] <- money == 100
}

mean(results)
## [1] 0.008
  1. Re-write this simulation in Python. In addition to the material we have covered so far, you will need to look up a couple new Python tools, such as setting a seed, and if...else... statements.

ChatGPT

A valuable tool for translating code from one language to another is ChatGPT. This can be helpful when you are learning a new language, because it can help you explore fundamental syntax and functions. However, be careful: if you don’t know a language well, it can be hard to check whether ChatGPT’s answer is reasonable!

  1. Ask ChatGPT to translate the R code above into Python. How do the results compare to your translation in question 1?