(五)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的更多相关文章

  1. 使用boost库生成 随机数 随机字符串

    #include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...

  2. boost高质量随机数库 zhuan

    shared_ptr<int> tmp2(new int(10)) ; int * test=tmp2.get(); std::cout<<*test<<" ...

  3. Boost 库uuid 的使用

    UUID 简介 通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分. uuid 版 ...

  4. boost库学习之开篇

    本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...

  5. boost库在windows下的编译和使用

    因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...

  6. Win7下Boost库的安装

    Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...

  7. (三)Boost库之字符串处理

    (三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i    : 有这个前缀表名算法的大小写不 ...

  8. (一)boost库之日期、时间

    (一)boost库之日期.时间 一.计时器  计时器,通常在一个项目中统计一个函数的执行时间是非常实用的.   #include <boost/timer.hpp> void PrintU ...

  9. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

随机推荐

  1. mysql null值转换

    1.如果为空返回0 select ifnull(null,0) 2.如果为空返回0,否则返回1 select if(isnull(col),0,1) as col.

  2. Mysql bigint 类型转为datetime

    最近在使用quartz,在mysql中其数据库表中的时间都是使用bigint类型存储的,要想使其查询结果显示为yyyy-mm-dd hh:MM:ss的格式需要使用 from_unixtime()函数, ...

  3. MIT-scheme安装

    下载地址: http://www.gnu.org/software/mit-scheme/ 下载windows版本,安装. The MIT-Scheme can be installed by jus ...

  4. UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>

    E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. UESTC_Little Deer and Blue Cat CDOJ 1025

    In DOTA, there are two Intellegence heroes. One is Enchantress, who is usually called Little Deer by ...

  6. Search Insert Position 解答

    Question Given a sorted array and a target value, return the index if the target is found. If not, r ...

  7. idea maven jetty插件热部署

    maven tomcat插件好像无法进行热部署,jetty可以如下配置实现热部署,但是idea无法进行自动编译,所以需要如下快捷键 Ctrl+Shift+F9,编译 Ctrl+F9,生成项目 < ...

  8. onvif规范的实现:onvif开发常用调试方法 和常见的segmentation fault错误

    在前几篇中,虽然已经实现了rtsp视频流的对接,但是还要做的工作还非常多,onvif本来就是一个覆盖面非常广的一个协议,每一个功能都要填充大量的函数.而且稍不注意就会出现segmentation fa ...

  9. ECShop2.7.2详细文件结构及模板结构目录名称

    ┣plugins目录┣templates目录┃   ┣backup目录┃   ┃   ┣index.htm┃   ┃   ┗ibrary目录┃   ┃       ┗index.htm┃   ┣cac ...

  10. FineUI

    window间传值: Window1.GetSaveStateReference(new string[] { TextBox1.ClientID, ak.ClientID })收 ActiveWin ...