// 忽略警告
#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;
}