无法理解为什么随机数算法给出不同的结果 [英] Cannot understand why random number algorithms give different results

查看:45
本文介绍了无法理解为什么随机数算法给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么以下 2 个代码片段会导致不同的输出,我希望有人能启发我.第二个版本稍微快一点——这是我的动机,因为这一切都在一个循环中,但我需要知道它是否等效,如果不是,那么为什么?

I'm trying to understand why the following 2 code snippets lead to different output and I'm hoping someone can enlighten me. The second version is marginally faster - which is my motivation since this all sits in a loop, but I need to know if it is equivalent and if not then why ?

    things <- 100000
    n.events <- 3
    probs <- rep(0.05, n.events)

    # Method 1
    set.seed(1)
    events1 <- matrix(n.events, nrow=things)
    events1 <- sapply(probs, function(probs) rbinom(things,1,probs))

    # Method 2
    set.seed(1)
    events2 <- matrix( rbinom(things*n.events, 1, probs), ncol=n.events,    byrow=TRUE)

    > identical(events1, events2)
    [1] FALSE

推荐答案

要得到同样的结果,你需要把 byrow=FALSE 而不是 byrow=TRUE for第二种选择.

To have the same result, you need to put byrow=FALSE instead of byrow=TRUE for the second option.

计算随机矩阵的第一种方法,它计算 rbinom(things,1,probs) length(probs) 次,每次获得一个 things 长向量.

the first way you're computing the random matrix, it computes rbinom(things,1,probs) length(probs) times, obtaining each time a things long vector.

要获得与第二种方法完全相同的矩阵,即计算 length(probs)*things 长向量,您需要在 length(probs) 长度向量 things.

To obtain the exact same matrix with the second way, which computes a length(probs)*things long vector, you need to "organize" it in length(probs) vectors of length things.

(无论哪种方式,种子在计算之间的处理"完全相同,因此获得的数字相同)

(either way, seed is "treated" exactly the same "in between" the computations so the numbers obtained are the same)

编辑

如果您想为向量 probs 使用不同的值,您需要以与 for 相同的顺序"使用概率的方式计算 events2events1 所以首先是向量 probs 的第一个值,直到达到所需的长度,然后是第二个值,依此类推,然后以与以前相同的方式组织矩阵的列,因此使用 byrow=FALSE.

If you want to use different values for the vector probs, you need to compute events2 in a way that the probs are used in the same "order" than for events1 so first the first value of vector probs, until the desired length is reached, then the second value, etc. and then, organize the columns of your matrix the same way as before, so using byrow=FALSE.

示例

# define the parameters:
things <- 100000
n.events <- 3
probs <- c(0.05, 0.01, 0.20)

# compute the random vectors with sapply
set.seed(1)
events1 <- sapply(probs, function(probs) rbinom(things,1,probs))

# compute the random vectors "directly", using the "right" probs
set.seed(1)
events2 <- matrix(rbinom(things*n.events, 1, rep(probs, e=things)), ncol=n.events,    byrow=FALSE)

# check the results
identical(events1, events2)
[1] TRUE

这篇关于无法理解为什么随机数算法给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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