简体   繁体   中英

how to conduct blackjack simulation with replacement

how would I Write code to simulate 5,000 repetitions of 2 cards being dealt to a player, whereby the cards are drawn with replacement. Using the relative freq of of a blackjack incidence In 5,000 repetitions of two cards being drawn (example; at a casino), provide an estimate of the prob of attaining a blackjack. Ive tried something like this:

set.seed(5000)
handValue = function(cards) {
  value = sum(cards)
  
       # Check for an Ace and change value if it doesn't bust
  if (any(cards == 1) && value <= 11) 
    value = value + 10
  
       # Check bust (set to 0); check Blackjack (set to 21.5)
  if(value > 21)  
    0 
  else if (value == 21 && length(cards) == 2)  
    21.5 # Blackjack
  else 
    value
}

But I'm not sure how to exactly simulate with replacement and this code was some rough ideas so it may be well off the mark.

How to simulate drawing 2 cards without replacement

sample(c(1:9,rep(10,4)),2,replace=F)

Edit : with replacement

sample(c(1:9,rep(10,4)),2,replace=T)

Edit2 : example with 5 replications

replicate(5,{sample(c(1:9,rep(10,4)),2,replace=T)})

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