boost 随机数

// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

#include <iostream>
#include <boost/random.hpp>
#include <boost/nondet_random.hpp>//random_device
#include <boost/serialization/singleton.hpp>
#include <boost/math/constants/constants.hpp>// 常量(π、e、√2等)
#include <boost/multiprecision/cpp_dec_float.hpp>// cpp_dec_float_100 更高精度
using namespace std;

class Rand
{
public:
    Rand() :rng(boost::mt19937(time(NULL))), ui(0, 255), vg(rng, ui)
    {
    }
    // 得到指定数量的随机数组
    void Rand_IntegerList(std::vector<uint32_t>& intVec,uint32_t num)
    {
        for (uint16_t i = 0; i < num; ++i)
        {
            intVec.push_back(GetRandNum());
        }
    }
    // 得到随机数
    uint32_t GetRandNum(){ return vg(); }
private:
    boost::mt19937 rng;            // 随机数发生器
    boost::uniform_int<> ui;    // 随机数分布器
    boost::variate_generator<boost::mt19937&, boost::uniform_int<> > vg;
};
typedef boost::serialization::singleton<Rand> sersing; 
#define Randoom boost::serialization::singleton<Rand>::get_mutable_instance()// 单例



int main()
{
    cout << "--------------------mt19937--------------------" << endl;
    boost::mt19937 rng1(time(NULL));// 以时间为种子创建一个随机数发生器
    for (int i = 0; i < 10; ++i)
        cout << rng1() << " "; //随机产生32位以内的正整数[0,2^23 - 1]
    cout << endl;


    cout << "--------------------uniform_smallint--------------------" << endl;
    // 在小整数域内均匀分布
    boost::uniform_smallint<> us(1, 100);
    for (int i = 0; i < 10; ++i)
        cout << us(rng1) << " ";
    cout << endl;


    cout << "--------------------uniform_int--------------------" << endl;
    // 在整数域上的均匀分布
    boost::uniform_int<> ui(0, 255);// 随机产生[0,255]的整数
    for (int i = 0; i < 10; ++i)
        cout << ui(rng1) << " "; 
    cout << endl;
    

    cout << "--------------------uniform_01--------------------" << endl;
    // 在区间[0,1]上的实数连续均匀分布
    boost::uniform_01<boost::mt19937 &> u01(rng1);
    for (int i = 0; i < 10; ++i)
        cout << u01() << " ";
    cout << endl;


    cout << "--------------------uniform_real--------------------" << endl;
    // 在区间[min,max]上的实数连续均匀分布
    boost::uniform_real<> ur(100,200);
    for (int i = 0; i < 10; ++i)
        cout << ur(rng1) << " ";
    cout << endl;

    cout << "--------------------variate_generator--------------------" << endl;
    // 变量发生器
    boost::variate_generator<boost::mt19937&, boost::uniform_int<> > vg(boost::mt19937(time(NULL)), boost::uniform_int<>(0,255));
    for (int i = 0; i < 10; ++i)
        cout << vg() << " ";
    cout << endl;


    cout << "--------------------serialization--------------------" << endl;
    for (int i = 0; i < 10; ++i)
        cout << sersing().get_mutable_instance().GetRandNum() << " ";
    cout << endl;

    sersing ss;
    for (int i = 0; i < 10; ++i)
        cout << ss.get_mutable_instance().GetRandNum() << " ";
    cout << endl;

    for (int i = 0; i < 10; ++i)
        cout << Randoom.GetRandNum() << " ";
    cout << endl;

    std::vector<uint32_t> intVec;
    int num = 10;
    Randoom.Rand_IntegerList(intVec, num);
    assert(intVec.size() == num);
    for (int i = 0; i < intVec.size(); ++i)
        cout << intVec[i] << " ";
    cout << endl;


    cout << "--------------------random_device--------------------" << endl;
    boost::random_device rd;
    for (int i = 0; i < 10; ++i)
        cout << rd() << " ";
    cout << endl;

    for(int i = 0; i < 10; ++i)
        cout << ui(rd) << " ";
    cout << endl;

    boost::variate_generator<boost::random_device&, boost::uniform_int<> > vg1(rd, ui);
    for(int i = 0; i < 10; ++i)
        cout << vg1() << " ";
    cout << endl;


    cout << "--------------------rand48--------------------" << endl;
    boost::rand48 rng2;
    for (int i = 0; i < 10; ++i)
    {
        uint32_t num = rng2();
        cout << num  << " "<< num * 1.0 << endl; //随机产生32位以内的正整数[0-2^23 - 1]
    }


    cout << "--------------------constants--------------------" << endl;
    using namespace boost::math;
    cout << float_constants::pi << endl;// 3.14159
    cout << double_constants::root_two << endl;// 1.41421
    cout << setprecision(64);
    cout << float_constants::pi << endl;//3.14159274101257320000000000000000000000000000
    cout << double_constants::root_two << endl;//1.41421356237309510000000000000000000000000000
    assert(constants::pi<float>() == float_constants::pi);
    
    using namespace boost::multiprecision;
    typedef cpp_dec_float_100 float_100;
    cout << constants::pi<float_100>() << endl; // 64位有效数字 3.141592653589793238462643383279502884197169399375105820974944592
    cout << setprecision(100);
    cout << constants::pi<float_100>() << endl;// 保留100位有效数字 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值