(五)boost库之随机数random
2016-03-15 14:35
411 查看
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 <iostream>
#include <boost/random.hpp>
#include <boost/random/random_device.hpp>
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<double> real(1, 5);
std::cout << real(gen) << std::endl;
}
{
//0-1上的实数
boost::uniform_01<boost::mt19937&> 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;
}
相关文章推荐
- 精度因子PDOP TDOP HDOP VDOP
- iOS : performSelectorOnMainThread
- C语言中压缩字符串的简单算法小结
- Hadoop MapReduceV2(Yarn) 框架简介
- (四)boost库之正则表达式regex
- POJ 1375 圆切线
- Enable Auto Deploy on vCenter Server Appliance (vCSA) 6
- 背包问题求解
- 一般背包问题
- python 列表推导式
- Android5.0新特性——兼容性(support)
- Android:带输入框的弹窗
- 类
- Servlet与Jsp的区别
- (三)Boost库之字符串处理
- iOS NSDate日期时间及NSTimer定时任务
- libcurl增加HTTP header 和 POST之后获取返回数据
- 活动安排
- 最优装载方案
- 计算机类资料查找