mat <- matrix(1:6)
# create a dice
n_simulations <- 10000000
# number of simulations
x <- 0
# number of simulations that sum = 12
for(i in 1:n_simulations){
random_number1 <- sample(c(mat), 1, replace = TRUE)
random_number2 <- sample(c(mat), 1, replace = TRUE)
random_number3 <- sample(c(mat), 1, replace = TRUE)
sum <- random_number1 + random_number2 + random_number3
if(sum == 12){
x <- x + 1
}
}
Q1 <- x / n_simulations
print(Q1)
## [1] 0.1157287
Q2 <- matrix(c(200, 200, 100, 200, 200, 300, 100, 200, 100 ,100), nrow = 5, ncol = 2)
#Q2 is the form
Row5sum <- sum(Q2[5, ])
# the number of people living in 'Others'
Totalsum <- sum(Q2)
Q2 <- Row5sum / Totalsum
print(Q2)
## [1] 0.1764706
- If the first card drawn is a diamond, then there are now 12 diamonds
left out of a total of 51 cards.
Q3 <- 12 / 51
print(Q3)
## [1] 0.2352941
Q4 <- choose(20, 10)
print(Q4)
## [1] 184756
Q5 <- 20 * 20 * 18
print(Q5)
## [1] 7200
Q6 <- factorial(10) / factorial((10 - 10))
print(Q6)
## [1] 3628800
- Event A: A coin is tossed 7 times.
Event B: A standard six-sided
die is rolled 3 times.
Event C: A group of four cards are drawn from
a standard deck of 52 cards without replacement
OutA <- 8
# OutA is the number of outcomes of event A.
OutB <- choose(6,3)
# OutB is the number of outcomes of event B.
OutC <- choose(52,4)
Q7 <- OutA * OutB * OutC
print(Q7)
## [1] 43316000
- Occupying alternate seats means the sequence must be MWMWMWMW or
WMWMWMWM. We need to calculate the number of one sequence and multiply
by two. What’s more, this table is ROUND. If everyone move one seat to
the right, the sequence is still the same. Every sequence can be moved
to seven sequences, which means we need to divided by 8 at last.
PM <- factorial(4) / factorial((4 - 4))
# PM is the permutation of men
PW <- factorial(4) / factorial((4 - 4))
# PM is the permutation of women
seq <- PM * PW
Q8 <- 2 * seq / 8
print(Q8)
## [1] 144
- Event A: withdraw one pancake and see that one side is brown.
Event B: the other side is brown.
So the answer should be P(B|A) =
P(AB)/P(A).
P(AB) equals to: withdraw one pancake that both sides
are brown.
PAB <- 1/3
# PAB is P(AB)
PA <- 3/6
# PA is P(A). There are six sides in total, and three are brown.
Q10 <- PAB / PA
print(Q10)
## [1] 0.6666667