Random - C++11, 7 of n

Since C++11, the C++ standard library provides a random-number library. The library provides many well-known distributions, and in addition, it provides multiple engines, which are the sources of randomness. To use the lib, include <random> header file.
1) The engines
serve as a stateful source of randomness. They are function objects that are able to generate random unsigned values uniformly distributed according to a predefined  minimum and maximum.Predefined engines:

Engine operations:

2) The distributions
serve as a way to specify how to use these random values to create random numbers that are distributed over a range of values according to user-supplied parameters.
Predefined distributions:

Distribution operations:

3) Use "random_device" as seed, mt19937 (mt19937_64) as generator, uniform_int_distribution<> as distributor. (fast, perfect uniform distribution)
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_int_distribution<int> dist(0, 99);
    for (int i = 0; i < 16; ++i) {
        std::cout << dist(mt) << " ";
    }
    std::cout << std::endl;

4) "random" considered harmful (non-uniform distributed). "random_shuffle" considered harmful (may call "random"). "shuffle" is awesome.

5) Example

#include <random>
#include <map>
#include <string>
#include <iostream>
#include <iomanip>
template <typename Distr, typename Eng>
void distr (Distr d, Eng e, const std::string& name)
{
    // print min, max and four example values
    std::cout << name << ":" << std::endl;
    std::cout << "- min(): " << d.min() << std::endl;
    std::cout << "- max(): " << d.max() << std::endl;
    std::cout << "- values: " << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << ’ ’ << d(e) << std::endl;
    // count the generated values (converted to integral values)
    std::map<long long,int> valuecounter;
    for (int i=0; i<200000; ++i) {
        valuecounter[d(e)]++;
    }
    // and print the resulting distribution
    std::cout << "====" << std::endl;
    for (auto elem : valuecounter) {
        std::cout << std::setw(3) << elem.first << ": " << elem.second << std::endl;
    }
    std::cout << "====" << std::endl;
    std::cout << std::endl;
}

int main()
{
    std::knuth_b e;
    std::uniform_real_distribution<> ud(0, 10);
    distr(ud,e,"uniform_real_distribution");
    std::normal_distribution<> nd;
    distr(nd,e,"normal_distribution");
    std::exponential_distribution<> ed;
    distr(ed,e,"exponential_distribution");
    std::gamma_distribution<> gd;
    distr(gd,e,"gamma_distribution");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值