简体   繁体   中英

Calculate the probabilities by simulation in R programming

You have all the black cards from a normal deck, I have all the red cards. We each choose one card from our half decks - the highest card wins. But I have managed to sneak out and throw away your 9 Of Clubs? Simulate 1000 games - how many did I win. (Ace is low.)

reds = rep(1:13, 2)
n = 1000
sum(sample(reds[-9], n, rep = T) < sample(reds, n, rep = T))

This is my code, I want to show the how many times I did win when simulate 1000 games if the 9 of clubs from black card removed.

If I understood correctly, you just need to divide the total of winnings by the total number of simulations

sum(sample(reds[-9], n, rep = T) < sample(reds, n, rep = T))/n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM