C++ 提升随机数生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2254909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boost random number generator
提问by shinjuo
Does anyone have a favorite boost random number generator and can you explain a little on how to implement it into code. I am trying to get the mersenne twister to work and was wondering if anyone had preference towards one of the others.
有没有人有最喜欢的 boost 随机数生成器,你能解释一下如何将它实现到代码中。我试图让梅森龙卷风工作,并想知道是否有人对其他人有偏好。
回答by
This code is adapted from the boost manual at http://www.boost.org/doc/libs/1_42_0/libs/random/index.html:
此代码改编自http://www.boost.org/doc/libs/1_42_0/libs/random/index.html 上的 boost 手册:
#include <iostream>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
using namespace std;
int main() {
typedef boost::mt19937 RNGType;
RNGType rng;
boost::uniform_int<> one_to_six( 1, 6 );
boost::variate_generator< RNGType, boost::uniform_int<> >
dice(rng, one_to_six);
for ( int i = 0; i < 6; i++ ) {
int n = dice();
cout << n << endl;
}
}
To explain the bits:
解释这些位:
mt19937is the mersenne twister generator,which generates the raw random numbers. A typedef is used here so you can easily change random number generator type.rngis an instance of the twister generator.one_to_sixis an instance of a distribution. This specifies the numbers we want to generate and the distribution they follow. Here we want 1 to 6, distributed evenly.diceis the thing that takes the raw numbers and the distribution, and creates for us the numbers we actually want.dice()is a call tooperator()for thediceobject, which gets the next random number following the distribution, simulating a random six-sided dice throw.
mt19937是梅森扭曲器生成器,它生成原始随机数。此处使用了 typedef,因此您可以轻松更改随机数生成器类型。rng是扭曲发生器的一个实例。one_to_six是分布的一个实例 。这指定了我们要生成的数字及其遵循的分布。这里我们想要 1 到 6 个,均匀分布。dice是获取原始数字和分布的东西,并为我们创建我们真正想要的数字。dice()是operator()对dice对象的调用,该对象获取分布后的下一个随机数,模拟随机的六面掷骰子。
As it stands, this code produces the same sequence of dice throws each time. You can randomise the generator in its constructor:
就目前而言,这段代码每次都会产生相同的掷骰子序列。您可以在其构造函数中随机化生成器:
RNGType rng( time(0) );
or by using its seed() member.
或通过使用其 seed() 成员。
回答by Jatin Kumar
I found this linkwhich gives a good overview of properties of different random number generators. I have copied the table from above link for convenience:
我发现这个链接很好地概述了不同随机数生成器的属性。为方便起见,我从上面的链接复制了表格:
+-----------------------+-------------------+-----------------------------+------------------------+ | generator | length of cycle | approx. memory requirements | approx. relative speed | +-----------------------+-------------------+-----------------------------+------------------------+ | minstd_rand | 2^31-2 | sizeof(int32_t) | 40 | | rand48 | 2^48-1 | sizeof(uint64_t) | 80 | | lrand48 (C library) | 2^48-1 | - | 20 | | ecuyer1988 | approx. 2^61 | 2*sizeof(int32_t) | 20 | | kreutzer1986 | ? | 1368*sizeof(uint32_t) | 60 | | hellekalek1995 | 2^31-1 | sizeof(int32_t) | 3 | | mt11213b | 2^11213-1 | 352*sizeof(uint32_t) | 100 | | mt19937 | 2^19937-1 | 625*sizeof(uint32_t) | 100 | | lagged_fibonacci607 | approx. 2^32000 | 607*sizeof(double) | 150 | | lagged_fibonacci1279 | approx. 2^67000 | 1279*sizeof(double) | 150 | | lagged_fibonacci2281 | approx. 2^120000 | 2281*sizeof(double) | 150 | | lagged_fibonacci3217 | approx. 2^170000 | 3217*sizeof(double) | 150 | | lagged_fibonacci4423 | approx. 2^230000 | 4423*sizeof(double) | 150 | | lagged_fibonacci9689 | approx. 2^510000 | 9689*sizeof(double) | 150 | | lagged_fibonacci19937 | approx. 2^1050000 | 19937*sizeof(double) | 150 | | lagged_fibonacci23209 | approx. 2^1200000 | 23209*sizeof(double) | 140 | | lagged_fibonacci44497 | approx. 2^2300000 | 44497*sizeof(double) | 60 | +-----------------------+-------------------+-----------------------------+------------------------+
length of cycle: length of random number sequence before it starts repeating
循环长度:开始重复之前的随机数序列的长度
回答by Jive Dadson
There's no one-size-fits-all RNG. Sometimes statistical properties are important, sometimes cryptology, sometimes raw speed.
没有一刀切的 RNG。有时统计属性很重要,有时是密码学,有时是原始速度。

