• <bdo id='5xAh7'></bdo><ul id='5xAh7'></ul>
    <legend id='5xAh7'><style id='5xAh7'><dir id='5xAh7'><q id='5xAh7'></q></dir></style></legend>

    1. <tfoot id='5xAh7'></tfoot>

      <small id='5xAh7'></small><noframes id='5xAh7'>

      <i id='5xAh7'><tr id='5xAh7'><dt id='5xAh7'><q id='5xAh7'><span id='5xAh7'><b id='5xAh7'><form id='5xAh7'><ins id='5xAh7'></ins><ul id='5xAh7'></ul><sub id='5xAh7'></sub></form><legend id='5xAh7'></legend><bdo id='5xAh7'><pre id='5xAh7'><center id='5xAh7'></center></pre></bdo></b><th id='5xAh7'></th></span></q></dt></tr></i><div id='5xAh7'><tfoot id='5xAh7'></tfoot><dl id='5xAh7'><fieldset id='5xAh7'></fieldset></dl></div>

        提升随机数生成器

        时间:2023-10-07
          <tbody id='JQNQo'></tbody>
        <legend id='JQNQo'><style id='JQNQo'><dir id='JQNQo'><q id='JQNQo'></q></dir></style></legend>

        <i id='JQNQo'><tr id='JQNQo'><dt id='JQNQo'><q id='JQNQo'><span id='JQNQo'><b id='JQNQo'><form id='JQNQo'><ins id='JQNQo'></ins><ul id='JQNQo'></ul><sub id='JQNQo'></sub></form><legend id='JQNQo'></legend><bdo id='JQNQo'><pre id='JQNQo'><center id='JQNQo'></center></pre></bdo></b><th id='JQNQo'></th></span></q></dt></tr></i><div id='JQNQo'><tfoot id='JQNQo'></tfoot><dl id='JQNQo'><fieldset id='JQNQo'></fieldset></dl></div>
          <tfoot id='JQNQo'></tfoot>
            <bdo id='JQNQo'></bdo><ul id='JQNQo'></ul>

              1. <small id='JQNQo'></small><noframes id='JQNQo'>

                • 本文介绍了提升随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有人有最喜欢的 boost 随机数生成器吗?你能解释一下如何将它实现到代码中吗?我试图让梅森龙卷风工作,并想知道是否有人偏爱其他人.

                  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.

                  推荐答案

                  此代码改编自 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 是梅森扭曲器生成器,它生成原始随机数.此处使用了 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 是twister 生成器的一个实例.

                  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 是获取原始数字和分布的东西,并为我们创建我们真正想要的数字.

                  dice is the thing that takes the raw numbers and the distribution, and creates for us the numbers we actually want.

                  dice() 是对 dice 对象的 operator() 调用,该对象获取紧随其后的下一个随机数分布,模拟随机六面掷骰子.

                  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.

                  就目前而言,这段代码每次都会产生相同的掷骰子序列.您可以在其构造函数中随机化生成器:

                  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) );   
                  

                  或使用其 seed() 成员.

                  or by using its seed() member.

                  这篇关于提升随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何从 rand() 缩小数字? 下一篇:在一个范围内生成无偏随机整数的最佳算法是什

                  相关文章

                  最新文章

                  1. <i id='DSV2A'><tr id='DSV2A'><dt id='DSV2A'><q id='DSV2A'><span id='DSV2A'><b id='DSV2A'><form id='DSV2A'><ins id='DSV2A'></ins><ul id='DSV2A'></ul><sub id='DSV2A'></sub></form><legend id='DSV2A'></legend><bdo id='DSV2A'><pre id='DSV2A'><center id='DSV2A'></center></pre></bdo></b><th id='DSV2A'></th></span></q></dt></tr></i><div id='DSV2A'><tfoot id='DSV2A'></tfoot><dl id='DSV2A'><fieldset id='DSV2A'></fieldset></dl></div>

                  2. <tfoot id='DSV2A'></tfoot>
                  3. <legend id='DSV2A'><style id='DSV2A'><dir id='DSV2A'><q id='DSV2A'></q></dir></style></legend>
                    • <bdo id='DSV2A'></bdo><ul id='DSV2A'></ul>

                    <small id='DSV2A'></small><noframes id='DSV2A'>