[英]Simulate a single n-sided die where the side with the highest number shows up twice as often as all other sides
I need to do this assignment. 我需要做这个任务。 I just don't know how it works.
我只是不知道它是如何工作的。 The question is.
问题是。
Modify the function roll() from the lecture in a way that it simulates a single n-sided die where the side with the highest number shows up twice as often as all other sides. 从演讲中修改函数roll(),模拟单个n面模具,其中具有最高编号的一侧显示的频率是所有其他边的两倍。 Functions you may find useful are ?, c(), min(), max(), length(), sort() and rep().
您可能会发现有用的函数是?,c(),min(),max(),length(),sort()和rep()。
And the function goes. 功能就是这样。
roll <- function( num = 1:6, rolls = 1) {
dice <- sample(num, size = rolls, replace = TRUE)
return(dice)
}
I'm pretty sure that i have to use the 'prob'-parameters in the sample-Function but i don't know how. 我很确定我必须使用示例函数中的'prob'参数,但我不知道如何。
You can do it without the prob
argument by thinking about what kind of fairly-weighted (all faces equally probable) die would give the results you want. 你可以在没有
prob
论证的情况下通过考虑什么样的公平加权(所有面孔同样可能)死亡来给出你想要的结果。
sample(1:6, 1)
gives you a single sample from an unbiased six-sided die. sample(1:6, 1)
从无偏的六面模具中提供单个样品。 What you seem to want in this instance is equivalent to a seven-sided die with two sixes. 在这种情况下你似乎想要的东西相当于一个有六个六面的七面模具。 Which would be...
哪个会......
sample(c(1:6,6),1)
That's an equal change of 1 to 5, and double the chance of a 6. 这是1到5的相等变化,是6的几率的两倍。
> table(sample(c(1:6,6),7000,replace=TRUE))
1 2 3 4 5 6
972 1018 1016 980 1018 1996
Its not clear to me whether "the highest number shows up twice as often as all other sides" means "all the other sides put together". 我不清楚“最高数字是否显示出所有其他方面的两倍”意味着“所有其他方面都放在一起”。 In which case you want to sample from a 10-sided die with 1 to 5 plus 5 sixes:
在这种情况下,您想从10面模具中采样1到5加5个六:
sample(c(1:5, rep(6,5)),1)
That's an equal chance of either getting 1 to 5 OR 6. 这是获得1比5或6的平等机会。
> table(sample(c(1:5, rep(6,5)),10000,replace=TRUE))
1 2 3 4 5 6
1012 961 943 1018 1026 5040
Generalise to N and write your function. 推广到N并编写您的函数。
You are right, the prob
-Parameter is useful here (eventhough you could do without). 你是对的,
prob
-Parameter在这里很有用(尽管你没有这个)。
Here are the steps you have to complete: 以下是您必须完成的步骤:
num
is largest (dont assume that it is the last) num
哪个条目最大(不要认为它是最后一个) ()
. ()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.