R中模拟骰子和硬币抛的结果 [英] Outcome of a simulated dice and coin toss in R

查看:86
本文介绍了R中模拟骰子和硬币抛的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该实验包括掷出一个公平的骰子并让 x 说,然后将一个公平的硬币抛 x 次并记录反面的数量.我需要做这个实验 50 次并将结果记录在一个向量中,(然后我将用它来绘制直方图.)

这是我目前的代码:

 for (i in 1:100){X <- 样本(6,1,replace=TRUE,c(1,1,1,1,1,1)/6)Y <- sample(2,1,replace=TRUE,c(1,1)/2)}Youtcomes <- c(sum(Y))青年队

但是我没有给我一个包含 100 个元素的向量,而是一直只得到一个数字.我哪里出错了?

注意:我必须使用 for 循环.

解决方案

Discalimer:(非常低效的解决方案参见 mnel/Gavin 的解决方案)

<块引用>

正如您可以阅读每个答案下面的许多,许多,..许多评论,而这个答案试图回答 OP 的具体问题(无论他的要求可能效率低下),本着维护论坛的礼仪,有些人(正确地)指出这个问题的品味很差,我的回答不符合论坛要求.我接受所有批评,仅出于显而易见的原因将答案留在这里(标记为答案,连续性).我建议您查看 mnel/Gavin 的答案,以获得针对这个特定问题的矢量化解决方案.如果您有兴趣查看 for 循环的实现,请参阅本文底部,但我建议您查看它以了解 for 循环的结构,但不要针对此特定实现 for 循环问题.谢谢.

<小时>

除了@Joshua 已经提到的主要问题之外,您的代码充满了很多问题:

首先,每次在循环内重写 X 和 Y 的值,因此在循环结束时,只有 Y 的最后一个值被求和.

其次,您的 Y 代码不正确.你说,你必须得到 x 次掷硬币,然而,你使用 sample(2, 1, ...).1 必须替换为 X ,它等于骰子卷中的数字.

试试这个代码:

Youtcomes <- sapply(1:100, function(x) {X <- 样本(1:6, 1, replace=TRUE, rep(1,6)/6)Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)总和(Y ==T")})

在这里,我们循环了 100 次,每次都对 1 到 6 之间的值进行采样并存储在 X 中.然后,我们对 head (H) 或 tail (T) X 次数并存储在 Y 中.

现在,sum(Y == "T") 给出 x 当前值的总和 (1 <= x <= 100).因此,最后,Youtcomes 将是您模拟的 Y == Tail 值集.

然后,你可以做一个hist(Youtcomes).

如果它是一个需要的 for 循环解决方案,

# 总是分配你将在 for 循环中索引的变量# 否则对象每次都会不断增长# 整个对象是为每个 i 创建的,这使得它非常# 缓慢/低效.Youtcomes <- rep(0, 100)对于(我在 1:100){X <- 样本(1:6, 1, replace=TRUE, rep(1,6)/6)Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)# 使用 [i] 索引分配循环内的输出Youtcomes[i] <- sum(Y == "T")# 因为 Youtcomes 之前被分配了 100 个 0 值# 值将在每个 i 处替换 0'.因此对象# 不是每次都复制.这更快/更有效.}

The experiment involves rolling a fair die and getting x say, then tossing a fair coin x number of times and recording the number of tails. I need to do this experiment 50 times and record the outcomes in a vector, (which I'll then use to plot a histogram.)

This is my code so far:

    for (i in 1:100)
    {X <- sample(6,1,replace=TRUE,c(1,1,1,1,1,1)/6)
    Y <- sample(2,1,replace=TRUE,c(1,1)/2)}
    Youtcomes <- c(sum(Y))
    Youtcomes

But instead of giving me a vector with 100 elements, I keep getting just a single number. Where am I going wrong?

Note: I have to use a for loop.

解决方案

Discalimer: (very inefficient solution see mnel/Gavin's solution)

As you can read the many, many, .. MANY comments underneath each of the answers, while this answer attempts to answer OP's specific question(however inefficient his requirements maybe), in the spirit of maintaining decorum of the forum, some have (rightly) pointed out that the question is in bad taste and my answer doesn't do justice to the forum requirements. I accept all criticism and leave the answer here only for obvious reasons (marked as answer, continuity). I suggest you look at mnel/Gavin's answer for a vectorised solution to this specific problem. If you're interested in looking at an implementation of for-loop, then refer to the bottom of this post, but I suggest you look at it to know the structure of for-loop, but not implement a for-loop to this specific problem. Thank you.


Your code is riddled with quite a few problems, apart from the main problem @Joshua already mentioned:

First, you rewrite every time the values of X and Y inside the loop so, at the end of the loop, there is only the last value of Y that is being summed up.

Second, your code for Y is not correct. You say, you have to get x amount of coin tosses, Yet, you use sample(2, 1, ...). The 1 must be replaced with X which equals the number from the die roll.

Try out this code instead:

Youtcomes <- sapply(1:100, function(x) {
    X <- sample(1:6, 1, replace=TRUE, rep(1,6)/6)
    Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)
    sum(Y == "T")
})

Here, we loop over 100 times, and each time, sample values between 1 and 6 and store in X. Then, we sample either head (H) or tail (T) X number of times and store in Y.

Now, sum(Y == "T") gives the sum for current value of x (1 <= x <= 100). So, at the end, Youtcomes will be your set of simulated Y == Tail values.

Then, you can do a hist(Youtcomes).

Edit: If its a for-loop solution that's desired then,

# always assign the variable you'll index inside for-loop
# else the object will keep growing every time and a copy of 
# entire object is made for every i, which makes it extremely 
# slow/inefficient.
Youtcomes <- rep(0, 100)
for (i in 1:100) {
    X <- sample(1:6, 1, replace=TRUE, rep(1,6)/6)
    Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)
    # assign output inside the loop with [i] indexing
    Youtcomes[i] <- sum(Y == "T")
    # since Youtcomes is assigned a 100 values of 0's before
    # the values will replace 0' at each i. Thus the object 
    # is not copied every time. This is faster/efficient.
}

这篇关于R中模拟骰子和硬币抛的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆