# This line of sample code simulates four random games where the Celtics either lose or win. Each game is independent of other games.
simulated_games <- sample(c("lose","win"), 4, replace = TRUE, prob = c(0.6, 0.4))
# The variable 'B' specifies the number of times I want the simulation to run. I will run the Monte Carlo simulation 10,000 times.
B <- 10000
# Use the `set.seed` function to make sure my answer matches the expected result after random sampling.
set.seed(1)
# Create an object called `celtic_wins` that first replicates the sample code generating the variable called `simulated_games` for `B` iterations and then tallies the number of simulated series that contain at least one win for the Celtics.
celtic_wins <- replicate(B, {simulated_games <- sample(c("lose","win"), 4, replace = TRUE, prob = c(0.6, 0.4)); any(simulated_games == c("win"))})
# Calculate the frequency out of B iterations that the Celtics won at least one game.
mean(celtic_wins)
#> [1] 0.8757