M <- 10 # number of people at the party
hats <- 1:M # numbered hats
nsim <- 10000 # number of simulations
results <- rep(0, nsim) # vector to store the results
for(i in 1:nsim){
randomized_hats <- sample(hats, M, replace = FALSE)
results[i] <- sum(randomized_hats == hats) > 0
}
mean(results)
[1] 0.6296