简体   繁体   English

模拟单个n侧模具,其中具有最高编号的一侧显示的频率是所有其他侧面的两倍

[英]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: 以下是您必须完成的步骤:

  • Find out which of the entries in num is largest (dont assume that it is the last) 找出num哪个条目最大(不要认为它是最后一个)
  • You need the index (="position") of that entry. 您需要该条目的索引(=“位置”)。
  • Calculate which probability each entry except the largest one would have. 计算除最大条目之外的每个条目的概率。 Example: If n=6 then each prob is 1/7 with the exception of the last which has 2/7. 示例:如果n = 6,那么每个概率为1/7,除了具有2/7的最后一个。
  • Make a vector containing these probabilities in the right positions. 在正确的位置创建包含这些概率的向量。 You already know the position of the largest, so you would put the doubled prob in that position. 你已经知道了最大的位置,所以你会把加倍的概率放在那个位置。
  • Give the prob to sample () . 将概率提供给sample ()
  • Test! 测试! Run it many times to find out if the largest is really approx. 多次运行以查明最大值是否真的大约。 double as often. 经常翻倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM