(五)boost库之随机数random
(五)boost库之随机数random
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;
}
(五)boost库之随机数random的更多相关文章
- 使用boost库生成 随机数 随机字符串
#include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...
- boost高质量随机数库 zhuan
shared_ptr<int> tmp2(new int(10)) ; int * test=tmp2.get(); std::cout<<*test<<" ...
- Boost 库uuid 的使用
UUID 简介 通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分. uuid 版 ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- boost库在windows下的编译和使用
因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...
- Win7下Boost库的安装
Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...
- (三)Boost库之字符串处理
(三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i : 有这个前缀表名算法的大小写不 ...
- (一)boost库之日期、时间
(一)boost库之日期.时间 一.计时器 计时器,通常在一个项目中统计一个函数的执行时间是非常实用的. #include <boost/timer.hpp> void PrintU ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
随机推荐
- 前台利用jcrop做头像选择预览,后台通过django利用Uploadify组件上传图最终使用PIL做图像裁切
之前一直使用python的PIL自定义裁切图片,今天有需求需要做一个前端的选择预览页面,索性就把这个功能整理一下,分享给大家. 实现思路: 1.前端页面: 用户选择本地一张图片,然后通过鼠标缩放和移动 ...
- ajax jquery return没有返回值
错误写法: function editdivisionmember(division_id,users_id){ $.ajax({ type:"POST", url:"/ ...
- LeeCode-Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** * Definition for a ...
- LeeCode-Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- curl 浏览器模拟请求实战
1,curl 常用选项
- Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- 449A - Jzzhu and Chocolate 贪心
一道贪心题,尽量横着切或竖着切,实在不行在交叉切 #include<iostream> #include<stdio.h> using namespace std; int m ...
- JS给元素循环添加事件的问题
<ul> <li>男</li> <li>女</li> <li>老</li> <li>少</li&g ...
- WebService调用1(.Net)
1.创建一个最简单的Web Service (1) 新建-项目-ASP.NET空WEB应用程序 (2)添加新项-WEB服务 默认会添加一个HelloWorld方法: using System; us ...
- NSNumber 和 NSValue 的部分使用
1.NSNumber 在Objective-c中有int,float,char等基本数据类型,但这些基本数据类型并不是对象,而数组,字典,字符串等容器中存放的都是对象类型,因此我们需要用到NSNumb ...