【问题标题】:create random number from a poisson dist. using MersenneTwister从泊松分布创建随机数。使用 MersenneTwister
【发布时间】:2017-07-05 07:08:05
【问题描述】:

您好,我正在运行一个模拟,我可以轻松地从均匀分布和正态分布中获取随机数:

#include <iostream>
#include "MersenneTwister.h"
using namespace std;
int main()
{
    MTRand mtrand1;
    double r1,r2;
    r1 = mtrand.rand(); // from a uninform dist.
    r2 = mtrand1.randNorm(); //from a normal dist.
}

我想使用这个随机数生成器从平均为“A”的泊松分布中获得一个随机数。

您知道如何使用 MersseneTwister 代码实现此过程吗? 代码可以在这里找到: https://gcc.gnu.org/bugzilla/attachment.cgi?id=11960。并且被广泛使用。

【问题讨论】:

  • 你知道这些都在random吗?
  • 如果你的意思是使用包,就没有必要使用了。如果不是,请澄清您的评论。
  • “不需要”是什么意思?该功能已经存在。是的,我的意思是标准库
  • 好的,如果你能提供解决方案,请这样做
  • 嗯....但是您的问题陈述是什么?你想要一个你想要使用的泊松分布吗?你想让采样算法形成泊松分布吗?您必须在问题中使用包含的标题吗?你想要什么?

标签: c++ poisson


【解决方案1】:

你可以使用标准库

#include<random>

double mean = 3.1415926;
std::mt19937 mt{std::random_device{}()};
std::poisson_distribution<> pd{mean};

auto n = pd(mt);  // get a number

请注意,使用std::random_device 播种是unlikely to be satisfactory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2016-01-31
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多