我如何使用Boost Random

时间:2018-02-02 22:11:34

标签: c++ boost boost-random

我需要使用Boost Random生成随机数。

我试图遵循一般指南。

我解压缩了库的文件。所以如果我想使用库的类和objectj我应该怎么做?

首先我知道在程序中包含库。然后我必须编译库和program.cpp本身? (并且使用相同的编译器 - 我正在使用g ++)。

我正在使用ubuntu的虚拟盒子。这是我第一次使用图书馆,所以我真的不知道。

1 个答案:

答案 0 :(得分:1)

  

我的案例的随机数必须是双倍而不仅仅是整数......

所以,你使用实数分布。

我不是这种"开始"最适合StackOverflow,但我会给你这个快速提示:

在您的Ubuntu虚拟框中:

sudo apt-get install libboost-all-dev
mkdir -pv ~/myproject
cd ~/myproject

使用您喜欢的编辑器创建文件。如果您没有,gedit main.cppnano main.cpp就是一个开头:

#include <boost/random.hpp>
#include <iostream>

int main() {
    boost::random::mt19937 rng;                                        
    boost::random::uniform_real_distribution<double> gen(0.0, 1.0);
    for (int i = 0; i < 10; ++i) {
        std::cout << gen(rng) << "\n";
    }
}

现在使用

编译它
g++ -O2 -Wall -Wextra -pedantic main.cpp -o demo

该计划现已准备就绪: Live On Coliru

./demo

打印

0.814724
0.135477
0.905792
0.835009
0.126987
0.968868
0.913376
0.221034
0.632359
0.308167

播种&amp;&amp;非标题库

以上是有效的,因为Boost随机库主要是标题。如果您想使用random_device实现为随机生成器播种,该怎么办?

<强> Live On Coliru

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

int main() {
    boost::random::random_device seeder;
    boost::random::mt19937 rng(seeder());                                        
    boost::random::uniform_real_distribution<double> gen(0.0, 1.0);
    for (int i = 0; i < 10; ++i) {
        std::cout << gen(rng) << "\n";
    }
}

现在您还必须链接:使用

进行编译
g++ -O2 -Wall -Wextra -pedantic main.cpp -o demo -lboost_random

现在每次运行输出都不同。

BONUS:标准库而不是Boost

你根本不需要提升:

<强> Live On Coliru

#include <random>
#include <iostream>

int main() {
    std::random_device seeder;
    std::mt19937 rng(seeder());                                        
    std::uniform_real_distribution<double> gen(0.0, 1.0);
    for (int i = 0; i < 10; ++i) {
        std::cout << gen(rng) << "\n";
    }
}

使用

进行编译
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo

使用./demo

再次运行它

奖金

显示具有均值= 0和stddev = 1的全部分布:

<强> Live On Coliru

#include <random>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <boost/serialization/array_wrapper.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>

namespace ba = boost::accumulators;
using Accum = ba::accumulator_set<double, ba::stats<ba::tag::variance, ba::tag::mean> >;
using Clock = std::chrono::high_resolution_clock;
using namespace std::chrono_literals;

static double identity(double d) { return d; }

template <typename Prng, typename Dist, typename F = double(double), size_t N = (1ull << 22)>
void test(Prng& rng, Dist dist, F f = &identity) {
    Accum accum;

    auto s = Clock::now();
    for (size_t i = 0; i<N; ++i)
        accum(f(dist(rng)));

    std::cout 
        << std::setw(34) << typeid(Dist).name() 
        << ":\t" << ba::mean(accum) 
        << " stddev: " << sqrt(ba::variance(accum)) 
        << " N=" << N 
        << " in " << ((Clock::now()-s)/1.s) << "s"
        << std::endl;
}

int main() {
    std::mt19937 rng(std::random_device{}());                                        

    auto shift = [](double shift) { return [=](double v) { return v + shift; }; };
    auto scale = [](double scale) { return [=](double v) { return v * scale; }; };

    std::cout << std::fixed << std::showpos;
    test(rng, std::uniform_real_distribution<double>(-sqrt(3), sqrt(3)));
    test(rng, std::weibull_distribution<double>(), shift(-1));
    test(rng, std::exponential_distribution<double>(), shift(-1));
    test(rng, std::normal_distribution<double>());
    test(rng, std::lognormal_distribution<double>(0, log(0.5)), shift(-exp(pow(log(0.5),2)/2)));
    test(rng, std::chi_squared_distribution<double>(0.5), shift(-0.5));
    {
        auto sigma = sqrt(6)/M_PI;
        static constexpr double ec = 0.57721566490153286060;
        test(rng, std::extreme_value_distribution<double>(-sigma*ec, sigma));
    }
    test(rng, std::fisher_f_distribution<double>(48, 8), shift(-(8.0/6.0)));
    test(rng, std::student_t_distribution<double>(4), scale(sqrt(0.5)));
    test(rng, std::student_t_distribution<double>(4), scale(sqrt(0.5)));
}

打印

  St25uniform_real_distributionIdE: +0.000375 stddev: +1.000056 N=4194304 in +0.169681s
       St20weibull_distributionIdE: +0.001030 stddev: +1.000518 N=4194304 in +0.385036s
   St24exponential_distributionIdE: -0.000360 stddev: +1.000343 N=4194304 in +0.389443s
        St19normal_distributionIdE: -0.000133 stddev: +1.000330 N=4194304 in +0.390235s
     St22lognormal_distributionIdE: +0.000887 stddev: +1.000372 N=4194304 in +0.521975s
   St24chi_squared_distributionIdE: -0.000092 stddev: +0.999695 N=4194304 in +1.233835s
 St26extreme_value_distributionIdE: -0.000381 stddev: +1.000242 N=4194304 in +0.611973s
      St21fisher_f_distributionIdE: -0.000073 stddev: +1.001588 N=4194304 in +1.326189s
     St22student_t_distributionIdE: +0.000957 stddev: +0.998087 N=4194304 in +1.080468s
     St22student_t_distributionIdE: +0.000677 stddev: +0.998786 N=4194304 in +1.079066s