boost :: random每次生成相同的数字

mon*_*ono 10 c++ random boost seed

主要.cpp

#include        "stdafx.h"
#include        "random_generator.h"


        int
main ( int argc, char *argv[] )
{
        cout.setf(ios::fixed);
        base_generator_type base_generator;
        int max = pow(10, 2);
        distribution_type dist(1, max);

        boost::variate_generator<base_generator_type&,
distribution_type > uni(base_generator, dist);
        for ( int i=0; i<10; i++ ) {
                //cout << random_number(2) << endl;
                cout << uni() << endl;
        }

        return EXIT_SUCCESS;

}                               /* ----------  end of function main  ---------- */
Run Code Online (Sandbox Code Playgroud)

random_gemerator.h

#include        "stdafx.h"

#include        <boost/random.hpp>
#include        <boost/generator_iterator.hpp>

typedef boost::mt19937 base_generator_type;
typedef boost::lagged_fibonacci19937 fibo_generator_type;
typedef boost::uniform_int<> distribution_type;
typedef boost::variate_generator<fibo_generator_type&,
distribution_type> gen_type;

        int
random_number ( int bits )
{
        fibo_generator_type fibo_generator;
        int max = pow(10, bits);
        distribution_type dist(1, max);

        gen_type uni(fibo_generator, dist);
        return uni();

}               /* -----  end of function random_number  ----- */
Run Code Online (Sandbox Code Playgroud)

stdafx.h中

 #include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;
Run Code Online (Sandbox Code Playgroud)

每次运行它都会生成相同的数字序列

像77,33,5,22 ......

如何使用boost:随机正确吗?


这就对了.但也许有一点问题,如下所示:

看起来很健康

get_seed(); for (;;) {cout << generate_random() << endl; } // is ok 
Run Code Online (Sandbox Code Playgroud)

它产生相同的随机数

int get_random() {get_seed();return generate_random();} for (;;) {cout << get_random() <<endl;}  // output the same random number yet
Run Code Online (Sandbox Code Playgroud)

Gre*_*osz 13

如果您希望每次运行程序时更改随机数序列,则需要通过使用当前时间初始化随机数来更改随机数

你会在那里找到一个例子,摘录:

/*
 * Change seed to something else.
 *
 * Caveat: std::time(0) is not a very good truly-random seed.  When
 * called in rapid succession, it could return the same values, and
 * thus the same random number sequences could ensue.  If not the same
 * values are returned, the values differ only slightly in the
 * lowest bits.  A linear congruential generator with a small factor
 * wrapped in a uniform_smallint (see experiment) will produce the same
 * values for the first few iterations.   This is because uniform_smallint
 * takes only the highest bits of the generator, and the generator itself
 * needs a few iterations to spread the initial entropy from the lowest bits
 * to the whole state.
 */
generator.seed(static_cast<unsigned int>(std::time(0)));
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢将PRNG初始化为`std :: time(NULL)+ getpid()`.这可以确保二进制文件一个接一个地快速连续执行,其PRNG的初始化方式不同. (8认同)

R S*_*hko 6

您需要为随机数生成器播种,因此每次都不会从同一个地方开始.

根据您对数字的处理方式,您可能需要考虑如何选择种子值.如果您需要高质量的随机性(如果您要生成加密密钥并希望它们相当安全),则需要良好的种子值.如果这是Posix,我会建议/ dev/random - 但是你看起来正在使用Windows,所以我不确定什么是好的种子来源.

但是如果你不介意可预测的种子(游戏,模拟等),快速而肮脏的种子是time()返回的当前时间戳.


Chr*_*ver 5

如果你在'nix系统上运行,你可以尝试这样的事情;

int getSeed()
{
    ifstream rand("/dev/urandom");
    char tmp[sizeof(int)];
    rand.read(tmp,sizeof(int));
    rand.close();
    int* number = reinterpret_cast<int*>(tmp);
    return (*number);
}
Run Code Online (Sandbox Code Playgroud)

我猜这种方式播种随机数生成器比简单地读取/dev/urandom(或/dev/random)所有随机数需求更快.