【发布时间】:2022-01-14 08:51:33
【问题描述】:
我正在尝试找出 r 中抽样生日为 0.9 (90%) 的最小人数
我正在尝试通过采样和两个 for 循环来做到这一点:
我认为我的预期结果是大约 300 人
bus = 2
#start person count at 2
count = 0
# create counter to count birthdays equal to jan first#assume no february 29th leap year
for (i in 1:400){
sims = 1000
for (i in 1:sims) {
bday = sample(1:365, bus, replace = TRUE) #randomly sample birthdays, producing bus number of samples
if (bday == 1) { # to see if there are two identical birthdays add to counter
count= count + 1
}
}
p= count/sims
print(p)
if(p<0.9){bus = bus+1}
if (p >= 0.9){bus = bus}
}
print(bus)
【问题讨论】:
-
您好,不太清楚您要做什么。你能澄清一下这个问题吗?如果这是一个概率问题,它可能不需要
for循环。无论如何,如果您要使用for,则此代码通常存在许多问题(即,两个for循环都使用i,您实际上并没有用i指定循环中的位置(例如,bday[i]),并且您不会在for循环之前初始化变量。
标签: r counter simulation probability