提升多精度随机生成器种子误差

Posted

技术标签:

【中文标题】提升多精度随机生成器种子误差【英文标题】:Boost Multiprecision random generator seed error 【发布时间】:2015-10-18 13:02:10 【问题描述】:

我一直在关注documentation from the boost library 来生成多精度随机整数,但在文档中没有提到如何设置种子。

我不知道如何在不出现编译错误的情况下设置种子。

#include <boost/multiprecision/gmp.hpp>     
#include <boost/multiprecision/random.hpp>  
#include <fstream>  

using namespace boost::multiprecision;
using namespace boost::random;
using namespace std;

//...    

typedef independent_bits_engine<mt19937, N_BITS, mpz_int> generator_type;

int main() 

    // ...

    generator_type gen;
    seed_seq sed_seq =  12064, 3867, 13555, 28676, 4599, 5031, 13040 ;
    gen.seed(sed_seq); 

    // ...

    for(unsigned i = 0; i < SS; ++i)  
     out
        << gen()
        << endl;
    

    out.close();
    return 0;

如何编译:

$ g++ generateinputs.cpp -o generateinputs.o -L/gmp_install/lib -lgmp

这是我得到的编译错误:

generateinputs.cpp: In function ‘int main()’:
generateinputs.cpp:35:68: error: in C++98 ‘sed_seq’ must be initialized by constructor, not by ‘...’
  seed_seq sed_seq =  12064, 3867, 13555, 28676, 4599, 5031, 13040 ;
                                                                    ^
generateinputs.cpp:35:68: error: could not convert ‘12064, 3867, 13555, 28676, 4599, 5031, 13040’ from ‘<brace-enclosed initializer list>’ to ‘boost::random::seed_seq’

我也试过用 c++11 编译,但还是有错误。你能告诉我它是怎么做的吗?

用 C++11 编译

$ g++ -std=c++11 generateinputs.cpp -o generateinputs.o -L/gmp_install/lib -lgmp 

结果:

generateinputs.cpp:26:9: error: reference to ‘independent_bits_engine’ is ambiguous
 typedef independent_bits_engine<mt19937, N_BITS, mpz_int> generator_type;

In file included from /usr/include/c++/4.8/random:50:0,
             from /usr/include/c++/4.8/bits/stl_algo.h:65,
             from /usr/include/c++/4.8/algorithm:62,
             from /usr/include/boost/math/tools/config.hpp:16,
             from /usr/include/boost/math/policies/error_handling.hpp:18,
             from /usr/include/boost/multiprecision/detail/default_ops.hpp:9,
             from /usr/include/boost/multiprecision/detail/generic_interconvert.hpp:9,
             from /usr/include/boost/multiprecision/number.hpp:22,
             from /usr/include/boost/multiprecision/gmp.hpp:9,
             from generateinputs.cpp:8:
/usr/include/c++/4.8/bits/random.h:1074:11: note: candidates are: template<class _RandomNumberEngine, long unsigned int __w, class _UIntType> class std::independent_bits_engine
 class independent_bits_engine

In file included from /usr/include/boost/random.hpp:38:0,
             from /usr/include/boost/multiprecision/random.hpp:31,
             from generateinputs.cpp:9:
/usr/include/boost/random/independent_bits.hpp:44:7: note:                 template<class Engine, long unsigned int w, class UIntType> class boost::random::independent_bits_engine
 class independent_bits_engine

generateinputs.cpp:26:9: error: ‘independent_bits_engine’ does not name a type
 typedef independent_bits_engine<mt19937, N_BITS, mpz_int> generator_type;

generateinputs.cpp: In function ‘int main()’:
generateinputs.cpp:33:6: error: ‘generator_type’ was not declared in this scope
  generator_type gen;

generateinputs.cpp:33:21: error: expected ‘;’ before ‘gen’
  generator_type gen;

generateinputs.cpp:36:2: error: reference to ‘seed_seq’ is ambiguous
seed_seq sed_seq =  12064, 3867, 13555, 28676, 4599, 5031, 13040 ;

In file included from /usr/include/c++/4.8/random:50:0,
             from /usr/include/c++/4.8/bits/stl_algo.h:65,
             from /usr/include/c++/4.8/algorithm:62,
             from /usr/include/boost/math/tools/config.hpp:16,
             from /usr/include/boost/math/policies/error_handling.hpp:18,
             from /usr/include/boost/multiprecision/detail/default_ops.hpp:9,
             from /usr/include/boost/multiprecision/detail/generic_interconvert.hpp:9,
             from /usr/include/boost/multiprecision/number.hpp:22,
             from /usr/include/boost/multiprecision/gmp.hpp:9,
             from generateinputs.cpp:8:
/usr/include/c++/4.8/bits/random.h:6025:9: note: candidates are: class     std::seed_seq
   class seed_seq

In file included from /usr/include/boost/random.hpp:53:0,
             from /usr/include/boost/multiprecision/random.hpp:31,
             from generateinputs.cpp:9:
/usr/include/boost/random/seed_seq.hpp:39:7: note:                 class boost::random::seed_seq
 class seed_seq 

generateinputs.cpp:36:11: error: expected ‘;’ before ‘sed_seq’
  seed_seq sed_seq =  12064, 3867, 13555, 28676, 4599, 5031, 13040 ;

generateinputs.cpp:37:2: error: ‘gen’ was not declared in this scope
  gen.seed(sed_seq); 

generateinputs.cpp:37:11: error: ‘sed_seq’ was not declared in this scope
  gen.seed(sed_seq); 

【问题讨论】:

确保将标志 -std=c++11 添加到编译命令中。 @vsoftco 我添加了关于 c++11 的信息 【参考方案1】:

您正在使用命名空间混合所有类型的操作系统。这会让编译器感到困惑(这是给我的):

Why is "using namespace std" considered bad practice?

此外,一些 boost 版本存在问题,需要特定顺序的多精度随机包含:

How to generate normal random numbers using boost multiprecision?

这是一个编译示例:

Live On Coliru

#include <boost/multiprecision/random.hpp>  
#include <boost/multiprecision/gmp.hpp>     
#include <fstream>  

using namespace boost::multiprecision;

//...    

typedef boost::random::independent_bits_engine<boost::mt19937, 64, mpz_int> generator_type;

int main() 

    // ...

    generator_type gen;
    boost::random::seed_seq sed_seq =  12064, 3867, 13555, 28676, 4599, 5031, 13040 ;
    gen.seed(sed_seq); 

    // ...

    for(unsigned i = 0; i < 100; ++i)  
        std::cout
            << gen()
            << std::endl;
    

打印

11603821463643999656
4488770058702608721
1099573171307694078
7494639050429105831
5349003296707484949
13868768316190520445
3211410294221291057
1325784923029427329
7032238616474679265
...

等等

【讨论】:

以上是关于提升多精度随机生成器种子误差的主要内容,如果未能解决你的问题,请参考以下文章

就生成随机数而言,种子是啥? [复制]

Oracle随机函数

在 Julia 随机数生成器中设置随机种子

使用连续种子生成统一的随机数?

哪些种子用于通用语言中的本机随机数生成器?

带有漂亮 API 的 Haskell 的纯伪随机生成器?