博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(五)boost库之随机数random
阅读量:7086 次
发布时间:2019-06-28

本文共 1475 字,大约阅读时间需要 4 分钟。

hot3.png

boost库为我们提供了许多的日常随机数生成器:

1.uniform_smallint:在小整数域内的均匀分布

2.uniform_int:在整数域上的均匀分布
3.uniform_01:在区间[0,1]上的实数连续均匀分布
4.uniform_real:在区间[min,max]上的实数连续均匀分布
5.bernoulli_distribution:伯努利分布
6.binomial_distribution:二项分布
7.cauchy_distribution:柯西(洛伦兹)分布
8.gamma_distribution:伽马分布
9.poisson_distribution:泊松分布
10.geometric_distribution:几何分布
11.triangle_distribution:三角分布
12.exponential_distribution:指数分布
13.normal_distribution:正态分布
14.lognormal_distribution:对数正态分布
15.uniform_on_sphere:球面均匀分布

 

随机数生成包括两部分,一是随机数种子,二是生成器,对于随机数种子,使用boost::random::mt19937就够用了

 
#include 
#include 
#include 
boost::random::mt19937 gen;
 
int _tmain(int argc, _TCHAR* argv[])
{
 
{
//整数
boost::uniform_int<> real(1, 999);
std::cout << real(gen) << std::endl;
}
 
{
//实数
boost::uniform_real
real(1, 5);
std::cout << real(gen) << std::endl;
}
 
{
//0-1上的实数
boost::uniform_01
u01(gen);
//正态分布,参数分别为均值、方差
boost::normal_distribution<> nd(0, 1);
std::cout << nd(u01) << std::endl;
}
 
boost::random::uniform_int_distribution<> dist(1, 1000);
std::cout << dist(gen) << std::endl;
std::cout << dist(gen) << std::endl;
 
std::string chars(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()"
"`~-_=+[{]{\\|;:'\",<.>/? ");
boost::random::random_device rng;
boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
for(int i = 0; i < 8; ++i) {
std::cout << chars[index_dist(rng)];
}
return 0;
}
 

转载于:https://my.oschina.net/lingluonianhua/blog/211795

你可能感兴趣的文章
关于同步访问与异步访问
查看>>
[LeetCode]题解(python):118-Pascal's Triangle
查看>>
HTML基础
查看>>
openstack部署之创建第一个实例
查看>>
2019年6月16日
查看>>
HDU4008 Parent and son(树形DP LCA)
查看>>
hdu 1058
查看>>
程序从sqlserver2008搬家到MySQL5.6
查看>>
开关电源串模扼流圈、共模扼流圈选择方法
查看>>
数-模(D/A)转换器
查看>>
第六十四课、c++中的异常处理(上)
查看>>
12/8团队会议7
查看>>
三分法
查看>>
ubuntun 18.04 安装和配置mysql数据库
查看>>
Javascript Design Patterns - Js Class
查看>>
计算机图形学 补 光线跟踪
查看>>
SDL1.3(C语言)程序移植LINUX。。。
查看>>
献给即将27岁的我
查看>>
spring整合logback配置文件
查看>>
FunRetroSnaker Privacy Description
查看>>