我如何使用 Boost Random

How do I use Boost Random

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

我试着遵循一般指南。

我提取了图书馆的文件。那么如果我想使用库的 类 和 objectj 应该怎么办呢?

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

我用的是ubuntu的虚拟盒子。我是第一次用库,所以我真的不知道。

the random number for my case must be double not just integer...

所以,你使用实数分布。


我不是那种 "getting started" 最适合 Whosebug 的人,但我会给你这个快速提示:

在您的 Ubuntu 虚拟框中:

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

使用您喜欢的编辑器创建一个文件。如果你有 none,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

播种 && Non-Header 仅库

以上的工作原理是因为 Boost Random 库主要是 header。如果您想使用 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";
    }
}

现在您还必须 link:使用

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

现在每个 运行 的输出都会不同。

奖励:标准库而不是 Boost

这里根本不需要 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 和标准差=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