Skip to content

Instantly share code, notes, and snippets.

@uchida
Created September 14, 2012 05:31
Show Gist options
  • Save uchida/3719981 to your computer and use it in GitHub Desktop.
Save uchida/3719981 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