国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

基于Boost庫生成隨機數

2019-11-08 02:54:24
字體:
來源:轉載
供稿:網友
#include <boost/random.hpp>#include <ctime>#include <iostream>#include <fstream>using namespace std;// 生成正態(tài)分布隨機數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( ){ // 正態(tài)分布樣本輸出#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 ) { // 正態(tài)分布測試#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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 望都县| 吉安市| 桂东县| 元朗区| 双桥区| 鹤壁市| 闽侯县| 农安县| 稻城县| 永安市| 漯河市| 怀远县| 库尔勒市| 灌南县| 长沙县| 钟山县| 庆云县| 泗洪县| 长治市| 台东市| 新龙县| 定州市| 江山市| 郴州市| 定西市| 定日县| 安平县| 梁山县| 峡江县| 新建县| 辰溪县| 九龙城区| 离岛区| 玉林市| 成都市| 景泰县| 五家渠市| 稻城县| 嘉黎县| 仙桃市| 罗平县|