用R找出机率

问题描述

a)在一个20人的小组中,有三个姐妹。该组在 随机分为两组,每组10个。 同一组? b)编写R代码以使用仿真解决问题。

我在B部分遇到困难。我不明白如何模拟这种情况

解决方法

这看起来像是一项家庭作业转储-最好花些力气,但似乎您不知道从哪里开始。

我已经注释了以下代码,以显示一种可能的方法

# Make the sample reproducible
set.seed(69)

# Calculate the probability that the brothers will be in the same group
prob_calculated <- 20/20 * 9/19 * 8/18

# Create our sample space. Here,1 stands for one of the brothers,# a zero means a non-brother:
space <- c(1,1,0)

# Create an empty vector to store the results of our simulation
vec <- logical()

# For 10,000 simulations,we will draw 10 numbers from space without replacement.
# If the sum of the sample is three or zero,the brothers are all in the same group.
# We store the result of each simulation in the vector vec at position i.
for(i in 1:10000) {
  samp <- sample(space,10)
  vec[i] <- sum(samp) == 0 | sum(samp) == 3
}

# The proportion of TRUE results in vec estmates the probability
prob_simulated <- length(which(vec))/10000

# Now we present the results for comparison
c(prob_calculated = prob_calculated,prob_simulated = prob_simulated)
#> prob_calculated  prob_simulated 
#>       0.2105263       0.2149000

reprex package(v0.3.0)于2020-10-10创建