I am trying to answer the following question: After the result of the simulation, can we see if this game had a unique winner? That is, was there a moment in which all other 999 players were eliminated but one player had still not been eliminated? (i.e. no ties)
Suppose we repeat this simulation 1000 times. Can we plot the result of each simulation and see how many winners each simulation had? (e.g. game 1 had 3 winners, game 2 had 1 winner, game 3 had 4 winners, etc)
Here is another solution using only base R, with a bar plot to answer the second question.
#
# Simulate a coin flipping game.
#
# First, set the random seed, the player count and the number of times to run the experiment.
set.seed(123)
n_players <- 1000
n_replications <- 1000
# Create a data frame to hold the results.
results <- data.frame(Iteration = 1:n_replications, Rounds = NA, Winners = NA, Unique = NA)
# Create a function to play the game once.
# INPUT: none
# OUTPUT: a list of results: number of rounds to extinction, number of winners, indicator
# that there was a unique winner(T) or not (F)
play <- function() {
round <- 0 # number of round just completed
alive <- n_players # number of players alive
while (alive > 0) { # play until nobody is alive
round <- round + 1 # update the round counter
# Sample {0, 1} (1 being a head) once for each currently alive player and count the number of heads.
a <- sample(0:1, size = alive, replace = TRUE, prob = c(0.5, 0.5)) |> sum()
if (a == 0) {
# If there are no survivors, we are done.
# The number of winners is the number alive entering this round.
return(list(Rounds = round, Winners = alive, Unique = (alive == 1)))
} else {
# Update the number of players alive and keep going.
alive <- a
}
}
}
# Play the game the desired number of times.
for (i in 1:n_replications) {
results[i, 2:4] <- play()
}
# Count the number of times there was a unique winner.
cat("Number of games with a unique winner = ", sum(results$Unique), ".\n")
# Plot the frequency of each number of winners.
barplot(height = table(results$Winners), xlab = "Winners", ylab = "Frequency")