二项式分布的随机数
我需要从二项式分布中快速生成大量随机数,以适应截然不同的试验规模(但是,大多数试验规模很小)。我希望不必手动编写算法(参见,例如 11 月份的相关讨论),因为我是一名新手程序员,不喜欢重新发明轮子。 Boost 似乎不提供二项分布变量的生成器,但 TR1 和 GSL 即可。是否有充分的理由选择其中之一,或者我根据自己的情况写一些定制的东西更好?我不知道这是否有意义,但我将在整个程序中交替从均匀分布和二项式分布生成数字,并且我希望它们共享相同的种子并最大限度地减少开销。我希望得到一些关于我应该考虑的建议或例子。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost 1.43 似乎支持 二项分布。您可以使用 boost::variate_generator 将随机源连接到类型
您想要从中采样的分布。
所以你的代码可能看起来像这样(免责声明:未经测试!):
Boost 1.43 appears to support binomial distributions. You can use
boost::variate_generator
to connect your source of randomness to the typeof distribution you want to sample from.
So your code might look something like this (Disclaimer: not tested!):
您误解了 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.