C++11--随机数使用
来源:互联网 发布:prototype js 闭包 编辑:程序博客网 时间:2024/05/21 07:55
随机数生成器
C++11提供了一个非确定数生成器random_device,用于产生高质量随机数
#include <iostream>#include <random>#define PRINTMSG( msg ) std::cout << msg << std::endlint main(){ std::random_device rd; for( int index =0; index < 20000; ++index ) { PRINTMSG( rd() ); } return 0; }
随机数引擎
用以生成随机数,其中包含生成引擎的状态
以下是3个引擎的列表
随机数引擎需要接受一个产生随机数的种子,该种子有random_device提供
#include <iostream>#include <random>#define PRINTMSG( msg ) std::cout << msg << std::endlint main(){std::random_device rd;// linear_congruential_engine std::mt19937// mersenne_twister_engine std::mt19937_64// subtract_with_carry_engine ranlux24_basestd::mt19937 mt(rd());for( int index =0; index < 20000; ++index ){PRINTMSG( mt() );}std::cout << mt() << std::endl;return 0;}
随机数分布
C++提供以下标准分布
分布名描述分布名描述分布名描述uniform_int_distribution离散型均匀分布poisson_distribution布瓦松分布exponential_distribution指数分布bernoulli_distribution伯努利分布binomial_distribution二项分布normal_distribution正态分布geometric_distribution几何分布uniform_real_distribution离散型均匀分布gamma_distribution伽马分布
各分布的使用也简单,如下代码
// 离散型均匀分布#include <iostream>#include <random>#define PRINTMSG( msg ) std::cout << msg << std::endlint main(){std::random_device rd;std::mt19937 gen(rd());// min() is 1, and max() is 6std::uniform_int_distribution<> dis(1, 6);for( int index = 0; index < 20; ++index ){PRINTMSG( dis( gen ) );}return 0;}
/*
* Blog Addr: http://blog.csdn.net/vsscusw
*
* Desc : Xiao Ma's Blog
*
* Author : XiaoMa
*
*/
- C语言随机数的使用
- C++11--随机数使用
- C语言的 “随机数” 使用技巧
- C语言--随机数函数的使用
- c语言随机数函数 rand()的使用
- 【Unity&C#&随机数】随机数
- 【C++】随机数
- 随机数使用
- 【c++】c++11中生成随机数
- 使用c生成随机数和matlab上的随机数相对应
- c++/c 产生随机数
- C/C++:随机数生成
- C/C++使用rand()和srand()函数产生随机数
- 使用C语言产生任意分布的随机数
- C语言如何产生一个随机数,rand函数的使用
- 【编程开发】C语言中随机数rand使用注意事项
- 使用 C / C++ 产生符合正态分布的随机数
- [C]如何产生随机数~
- 关于java垃圾回收问题
- Ubuntu安装徽软雅黑字体
- Linux系统函数之fcntl函数
- 如何将多个文件捆绑成一个可执行文件
- Android---控制多媒体应用程序的音量和播放
- C++11--随机数使用
- /dev/shm 分析
- OpenCV2.4.4 图像旋转和缩放
- 如何将一个文件分割成多个小文件
- 解决Fedora没有最大化最小化按钮
- axis2 创建服务端 客户端
- android 资源文件介绍
- Tomcat6.0配置
- QP学习-内核