提高随机数发生器 [英] Boost random number generator
问题描述
有没有人有一个最喜欢的提振随机数生成器,你可以解释如何落实到code一点点。我试图让梅森倍捻机工作,并想知道如果任何人有preference对别人的一个。
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.
推荐答案
这code从升压手动调整在<一个href=\"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:
This code is adapted from the boost manual at http://www.boost.org/doc/libs/1_42_0/libs/random/index.html:
#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;
}
}
要解释位:
-
mt19937
是梅森倍捻机发生器,它产生的原始随机数。 v一个typedef用在这里,因此您可以轻松地改变随机数生成器类型。
mt19937
is the mersenne twister generator,which generates the raw random numbers. A typedef is used here so you can easily change random number generator type.
RNG
是龙卷风生成器的实例。
rng
is an instance of the twister generator.
one_to_six
是分配的实例。这指定我们要生成的数量和他们遵循的分布。这里我们要1至6个,分布均匀。
one_to_six
is 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.
骰子
是取原始数据和分布,为我们创造,我们真正想要的数字的东西。
dice
is the thing that takes the raw numbers and the distribution, and creates for us the numbers we actually want.
骰子()
是 A调用运算符()
为骰子
对象,它获取下一个随机数的分布如下,模拟一个随机的六面骰子掷。
dice()
is a call to operator()
for the dice
object, which gets the next random number following the distribution, simulating a random six-sided dice throw.
因为它的立场,这code产生骰子的相同顺序每次罚球。您可以随机化发生器在其构造:
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.
这篇关于提高随机数发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!