c++ - 随机 boost 独立性

标签 c++ boost random

我正在尝试使用 mersenne twister 从各种分布中生成样本。我有一个生成器,它用于生成所有这些生成器。这里发生了一些奇怪的事情(至少对我而言)。一方面,计算各种样本的相关系数几乎为零,这看起来不错。但是,当我更改一个分布的参数(在其他任何地方都用不到)时,它也会以某种方式改变我在其他分布中获得的结果。具体来说:

#include <boost/random.hpp>

using namespace boost; // boost random library for random generators

mt19937 generator(7687); // mersenne twister random number generator, seed = 7687

double normal_sample(double mu, double sigma)
// returns a sample from normal distribution with mean mu and variance sigma 
{
    normal_distribution<> norm_dist;
    variate_generator<mt19937&, normal_distribution<> > norm_rnd(generator, norm_dist);

    return(mu + sigma * norm_rnd());
}    

double poisson_sample(double intensity)
// returns a number of points in a realization of a Poisson point process
{
    poisson_distribution<> poiss_dist(intensity);
    variate_generator<mt19937&, poisson_distribution<> > poiss_rnd(generator, poiss_dist);

    return(poiss_rnd());
}

这是代码...生成器部分,然后我从这两个分布中提取,更改名为 intensity 的参数。这不仅改变了泊松样本,也改变了正常样本……实际上,现在我想到了,这有点道理,因为我的泊松样本确定了一些点,这些点也是使用同一个生成器随机生成的...然后根据它们的数量,我得到了其他东西,因为正常样本是使用序列中的不同数字生成的。对吗?

如果是这样,人们将如何改变它?我应该使用多个生成器吗?

最佳答案

这可能意味着根据参数的不同,从梅森扭曲器中提取的随机样本会更少或更多。

这在逻辑上意味着所有其他结果都发生了变化,从而使所有其他结果都不同。

[...] it kind of makes sense, because my Poisson sample determines a number of points that are also randomly generated using the same generator...so then then depending on how many of them there are, I get something else, because the normal sample is generated using different numbers in the sequence. Is that correct?

在我看来你已经想通了,是的。

如果您想要可重复的 PRNG,请使用单独的 PRNG 状态,即不同的 mersenne egnines。

关于c++ - 随机 boost 独立性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471653/

相关文章:

c++ - MySQL C++ 连接器内存溢出错误

c++ - Boost::asio 阻塞 Tcp 服务器困惑

c++ - Debian 测试中缺少 `/usr/lib/libboost_unit_test_framework-mt.so'

php - 随机 MySQL 查询?或者随机化一个结果?

javascript - 显示随机 div 的框

java - 生成固定长度的随机数组

c++ - STL maps中的key是如何升序排列的?

c++ - 匿名结构内部循环

C++ 守护进程不会接收 SIGCHLD 信号

list - 将 C++ 创建的对象追加到 python 列表并使其由 python 管理