基于Boost库生成随机数
来源:互联网 发布:2钻淘宝店铺转让多少钱 编辑:程序博客网 时间:2024/06/10 01:14
#include <boost/random.hpp>#include <ctime>#include <iostream>#include <fstream>using namespace std;// 生成正态分布随机数double sampleNormal( double mean, double sigma ){ // 建立Mersenne twister随机数生成器 // 使用Unix时间设定seed static boost::mt19937 rng( static_cast< unsigned >( std::time( 0 ) ) ); // 选择高斯随机分布 boost::normal_distribution< double > norm_dist( mean, sigma ); // 使用function的形式,生成随机数产生器 boost::variate_generator< boost::mt19937&, boost::normal_distribution< double > > normal_sampler( rng, norm_dist ); return normal_sampler( );}// 生成一定范围内的随机整数// 值域是[start, end]int sampleUniformInt( int start, int end ){ static boost::mt19937 rng( static_cast< unsigned >( std::time( 0 ) ) ); boost::uniform_int< > uni_dist( start, end ); return uni_dist( rng );}// 生成一定范围内的随机实数double sampleUniformDouble( double start, double end ){ static boost::mt19937 rng( static_cast< unsigned >( std::time( 0 ) ) ); boost::uniform_real< > uni_dist( start, end ); return uni_dist( rng );}#define DEMO_NORMAL_DIST 1#define DEMO_UNI_DIST_INT 0#define DEMO_UNI_DIST_DOUBLE 0int main( ){ // 正态分布样本输出#if DEMO_NORMAL_DIST ofstream o_normal_distribution_file( "normal_distribution.txt" );#endif#if DEMO_UNI_DIST_INT ofstream o_uni_distribution_int_file( "uni_distribution_int.txt" );#endif#if DEMO_UNI_DIST_DOUBLE ofstream o_uni_distribution_double_file( "uni_distribution_double.txt" );#endif while ( 1 ) { // 正态分布测试#if DEMO_NORMAL_DIST cout << sampleNormal( 10, 0.1 ) << endl; o_normal_distribution_file << sampleNormal( 10, 0.1 ) << endl;#endif // 整数均匀分布测试#if DEMO_UNI_DIST_INT cout << sampleUniformInt( 0, 9 ) << endl; o_uni_distribution_int_file << sampleUniformInt( 0, 9 ) << endl;#endif // 实数均匀分布测试#if DEMO_UNI_DIST_DOUBLE cout << sampleUniformDouble( 0, 1 ) << endl; o_uni_distribution_double_file << sampleUniformDouble( 0, 1 ) << endl;#endif } return 0;}
0 0
- 基于Boost库生成随机数
- 使用boost库生成 随机数 随机字符串
- 【Boost】boost库的随机数的例子
- 【Boost】boost库的随机数的例子
- 【Boost】boost库的随机数的例子
- boost高质量随机数库
- Boost 随机数库的使用
- 基于vs2015 64位 boost python库的生成
- boost随机数
- 基于多线程的随机数生成算法
- boost库生成
- boost库的随机数的例子
- (五)boost库之随机数random
- Boost产生随机数
- boost 随机数产生
- 生成VS2010的BOOST库
- 【随机数】生成随机数模板
- 生成随机数
- sql语句关键字学习网站
- Docker 从入门到精通(二) 搭建本地仓库
- android jni环境搭建之下载安装Android SDK
- 什么是类?什么是对象?类和对象有什么关系?
- dubbo负载均衡策略(面试问题:dubbo负载均衡是怎么配置的)
- 基于Boost库生成随机数
- 矩阵特征值分解与奇异值分解含义解析及应用
- 如何查看Nginx的PID
- sql游标小记
- Javascript的简单测试环境
- freemarker中map的创建以及map的遍历循环
- 要切换到hdfs用户下运行jar
- 4、spring使用@Autowired注解实现自动装配
- 汇编中计算机基本原理部分名词缩写解析