C++ 如何使用Boost Random
我需要用Boost random生成随机数 我试着按照一般指南去做 我提取了图书馆的文件。因此,如果我想使用库的类和objectj,我应该怎么做 首先,我知道在程序中包括库。然后我必须编译库和程序.cpp本身?(并且都使用相同的编译器-我使用的是g++) 我使用的是ubuntu的虚拟盒子。这是我第一次使用图书馆,所以我真的不知道C++ 如何使用Boost Random,c++,boost,boost-random,C++,Boost,Boost Random,我需要用Boost random生成随机数 我试着按照一般指南去做 我提取了图书馆的文件。因此,如果我想使用库的类和objectj,我应该怎么做 首先,我知道在程序中包括库。然后我必须编译库和程序.cpp本身?(并且都使用相同的编译器-我使用的是g++) 我使用的是ubuntu的虚拟盒子。这是我第一次使用图书馆,所以我真的不知道 我的案例中的随机数必须是双精度的,而不仅仅是整数 所以,使用实数分布 我不认为这种“入门”最适合StackOverflow,但我会给你一些提示: 在您的Ubuntu虚
我的案例中的随机数必须是双精度的,而不仅仅是整数 所以,使用实数分布
我不认为这种“入门”最适合StackOverflow,但我会给你一些提示: 在您的Ubuntu虚拟框中:
sudo apt-get install libboost-all-dev
mkdir -pv ~/myproject
cd ~/myproject
使用您喜爱的编辑器创建文件。如果没有,gedit main.cpp
或nano 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";
}
}
程序现在可以运行了:
./demo
#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";
}
}
#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";
}
}
#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)));
}
印刷品
0.814724
0.135477
0.905792
0.835009
0.126987
0.968868
0.913376
0.221034
0.632359
0.308167
设定种子(&N)仅限页眉的库
上述方法之所以有效,是因为Boost随机库基本上是只包含头的。如果您想使用random\u设备
实现为随机生成器播种,该怎么办
./demo
#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";
}
}
#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";
}
}
#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)));
}
现在,每次运行的输出都将不同
奖励:标准库而不是Boost
在这里,您根本不需要增压:
./demo
#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";
}
}
#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";
}
}
#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)));
}
然后使用/demo
奖金
显示平均值为0且STDEV=1的整个分布范围:
./demo
#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";
}
}
#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";
}
}
#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)));
}
有什么特别不适合你?因为你的问题太广泛,我们这里不写一步一步的教程。还要注意,这是当前标准库的一部分。如果你有一个编译器,至少支持C++ 11,(这是目前C++编译器中绝大多数)在本手册第6部分中,您应该更喜欢标准的
标题而不是
guidehttp://www.boost.org/doc/libs/1_66_0/more/getting_started/unix-variants.html 举个例子,我没能成功地复制他们解释的链接到库的两种主要方式,因为到目前为止我真的知道一点。所以我想知道对于Geneall库,比如boots库,如何链接和使用它。我的例子中的随机数必须是双精度的,而不仅仅是整数…我如何生成一个平均值为零,标准偏差为1的随机数?你使用一个满足它的分布。有很多。查看Boost AFAIK中存在的所有这些(因此,如果您必须使用Boost,则没有什么区别)只是为了好玩,添加了10个具有这些属性的不同发行版:。选择正态分布,除非您知道自己在做什么:)boost::random::mt19937
可以使用boost::random::mt19937 rng(播种机)播种32位以上的种子材料代码>(注意缺少参数)。这是因为boost::random::random_设备
有一个.generate
成员函数,可以用作种子序列。