(五)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;
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值