'writing code to simulate 5000 repetitions of two cards in a blackjack game where cards are drawn with replacement [closed]
I want to write code to simulate 5000 repetitions of two cards in a blackjack game where cards are drawn with replacement. I'm having issues writing code to simulate this game where I'm looking for the probability for getting a blackjack.
Here's what I've done so far.
library(dplyr)
library(tidyverse)
set.seed(123)
cards <-tibble(repetition = 1:5000,
card1 = sample(1:52, size = 5000, replace = TRUE),
card2 = sample(1:52, size = 5000, replace = TRUE))
blackjack <-cards %>%
mutate(blackjack = if_else((card1 %in% c(1,10,19,28,37,46)) and
(card2 %in% c(1,10,19,28,37,46)),
"Blackjack", "Not blackjack"))
blackjack %>%
count(blackjack) %>%
mutate(proportion = n/5000)
But the initial error is in:
Error: unexpected symbol in:
"blackjack <-cards %>%
mutate(blackjack = if_else((card1 %in% c(1,10,19,28,37,46)) and"
Solution 1:[1]
I agree with other answers that there are better ways to go about this, but if you want to persevere with your simulation with minimal edits:
library(dplyr)
library(tidyverse)
set.seed(123)
suit=c(11,2:9,rep(10,4)) ##ace = 11, other cards = 2:9, 10 and pictures cards = 10
##NOTE, you only need to use one suit as you are simulating multiple decks the odds are the same
cards <-tibble(repetition = 1:5000,
card1 = sample(suit, size = 5000, replace = TRUE),
card2 = sample(suit, size = 5000, replace = TRUE))
blackjack <-cards %>%
mutate(blackjack = if_else((card1 + card2 == 21),
"Blackjack", "Not blackjack"))
blackjack %>%
count(blackjack) %>%
mutate(proportion = n/5000) ##note, 5000 iterations, not 10000
# A tibble: 2 × 3
blackjack n proportion
<chr> <int> <dbl>
1 Blackjack 249 0.0498
2 Not blackjack 4751 0.950
N.B. I am assuming this is a simulation of multiple deck blackjack
Solution 2:[2]
There is a simpler way to emulate this, assuming that you are trying to emulate the game of blackjack.
Since the cards are drawn with replacement, the probability of getting a 10-valued card (i.e. a 10 or a face card) is 16 / 52 (since there are four 10s, four Jacks, four Queens and four Kings in a deck). The probability of getting an Ace is 4 / 52, and getting a plain card is therefore 32 / 52.
Let us create a vector of these possibilities, and set our number of draws:
poss <- c(Ten = 16/52, Ace = 4/52, Plain = 32/52)
draws <- 10000
Now we can create an array of 10,000 draws like this:
set.seed(123)
cards <- replicate(2, sample(names(poss), draws, TRUE, prob = poss))
head(cards)
#> [,1] [,2]
#> [1,] "Plain" "Plain"
#> [2,] "Ten" "Plain"
#> [3,] "Plain" "Ten"
#> [4,] "Ten" "Plain"
#> [5,] "Ace" "Plain"
#> [6,] "Plain" "Plain"
We can test whether each row is a blackjack by using apply
is_blackjack <- apply(cards, 1, function(x) {
(x[1] == 'Ace' & x[2] == 'Ten') |
(x[1] == 'Ten' & x[2] == 'Ace')
})
And dividing the sum of this vector by the number of draws gives us our estimated probability:
sum(is_blackjack) / draws
#> [1] 0.0462
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Allan Cameron |
