二项式分布的随机数

发布于 2024-08-31 22:05:29 字数 606 浏览 4 评论 0原文

我需要从二项式分布中快速生成大量随机数,以适应截然不同的试验规模(但是,大多数试验规模很小)。我希望不必手动编写算法(参见,例如 11 月份的相关讨论),因为我是一名新手程序员,不喜欢重新发明轮子。 Boost 似乎不提供二项分布变量的生成器,但 TR1GSL 即可。是否有充分的理由选择其中之一,或者我根据自己的情况写一些定制的东西更好?我不知道这是否有意义,但我将在整个程序中交替从均匀分布和二项式分布生成数字,并且我希望它们共享相同的种子并最大限度地减少开销。我希望得到一些关于我应该考虑的建议或例子。

I need to generate quickly lots of random numbers from binomial distributions for dramatically different trial sizes (most, however, will be small). I was hoping not to have to code an algorithm by hand (see, e.g., this related discussion from November), because I'm a novice programmer and don't like reinventing wheels. It appears Boost does not supply a generator for binomially distributed variates, but TR1 and GSL do. Is there a good reason to choose one over the other, or is it better that I write something customized to my situation? I don't know if this makes sense, but I'll alternate between generating numbers from uniform distributions and binomial distributions throughout the program, and I'd like for them to share the same seed and to minimize overhead. I'd love some advice or examples for what I should be considering.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2024-09-07 22:05:29

Boost 1.43 似乎支持 二项分布。您可以使用 boost::variate_generator 将随机源连接到类型
您想要从中采样的分布。

所以你的代码可能看起来像这样(免责声明:未经测试!):

boost::mt19937 rng;                 // produces randomness out of thin air
                                    // see pseudo-random number generators
const int n = 20;
const double p = 0.5;
boost::binomial<> my_binomial(n,p);      // binomial distribution with n=20, p=0.5
                                         // see random number distributions
boost::variate_generator<boost::mt19937&, boost::binomial<> >
         next_value(rng, my_binomial);     // glues randomness with mapping
int x = next_value();                      // simulate flipping a fair coin 20 times

Boost 1.43 appears to support binomial distributions. You can use boost::variate_generator to connect your source of randomness to the type
of distribution you want to sample from.

So your code might look something like this (Disclaimer: not tested!):

boost::mt19937 rng;                 // produces randomness out of thin air
                                    // see pseudo-random number generators
const int n = 20;
const double p = 0.5;
boost::binomial<> my_binomial(n,p);      // binomial distribution with n=20, p=0.5
                                         // see random number distributions
boost::variate_generator<boost::mt19937&, boost::binomial<> >
         next_value(rng, my_binomial);     // glues randomness with mapping
int x = next_value();                      // simulate flipping a fair coin 20 times
始终不够 2024-09-07 22:05:29

您误解了 Boost 模型 - 您选择随机数生成器类型,然后选择一个分布,将 RNG 生成的值分散到该分布上。 这个答案中有一个非常简单的例子,它使用均匀分布,但其他分布使用相同的基本模式 - 生成器和分布完全解耦。

You misunderstand the Boost model - you choose a random number generator type and then a distribution on which to spread the values the RNG produces over. There's a very simple example in this answer, which uses a uniform distribution, but other distributions use the same basic pattern - the generator and the distribution are completely decoupled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文