Skip to content

Instantly share code, notes, and snippets.

@seiriosPlus
Forked from uchida/rand.cpp
Created August 6, 2018 14:15
Show Gist options
  • Save seiriosPlus/e3de8370195373a2f4cb8303773d4047 to your computer and use it in GitHub Desktop.
Save seiriosPlus/e3de8370195373a2f4cb8303773d4047 to your computer and use it in GitHub Desktop.
boost random distribution usage sample
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include <boost/random/normal_distribution.hpp>
int main(int argc, char* argv[])
{
size_t seed = 1234567890;
boost::random::mt19937 engine(seed);
boost::function<double()> randu =
boost::bind(boost::random::uniform_real_distribution<>(0, 1), engine);
std::cout << "sampling 100 from uniform distribution" << std::endl;
for (size_t i = 0; i < 100; ++i) {
std::cout << randu() << std::endl;
}
boost::function<double()> randn =
boost::bind(boost::random::normal_distribution<>(0, 1), engine);
std::cout << "sampling 100 from normal distribution" << std::endl;
for (size_t i = 0; i < 100; ++i) {
std::cout << randn() << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment