c++ - Random Number Generator with Beta Distribution -
i need c or c++ source code of function betarand(a,b) produces random number beta distribution . know can use boost library i'm going port cuda architecture need code. can me?
meantime have betapdf(beta probability density function). don't know how use creating random numbers :).
the c++11 random number library doesn't provide beta distribution. however, beta distribution can modelled in terms of 2 gamma distributions, library does provide. i've implemented beta_distribution in terms of std::gamma_distribution you. far can tell, conforms requirements random number distribution.
#include <iostream> #include <sstream> #include <string> #include <random> namespace sftrabbit { template <typename realtype = double> class beta_distribution { public: typedef realtype result_type; class param_type { public: typedef beta_distribution distribution_type; explicit param_type(realtype = 2.0, realtype b = 2.0) : a_param(a), b_param(b) { } realtype a() const { return a_param; } realtype b() const { return b_param; } bool operator==(const param_type& other) const { return (a_param == other.a_param && b_param == other.b_param); } bool operator!=(const param_type& other) const { return !(*this == other); } private: realtype a_param, b_param; }; explicit beta_distribution(realtype = 2.0, realtype b = 2.0) : a_gamma(a), b_gamma(b) { } explicit beta_distribution(const param_type& param) : a_gamma(param.a()), b_gamma(param.b()) { } void reset() { } param_type param() const { return param_type(a(), b()); } void param(const param_type& param) { a_gamma = gamma_dist_type(param.a()); b_gamma = gamma_dist_type(param.b()); } template <typename urng> result_type operator()(urng& engine) { return generate(engine, a_gamma, b_gamma); } template <typename urng> result_type operator()(urng& engine, const param_type& param) { gamma_dist_type a_param_gamma(param.a()), b_param_gamma(param.b()); return generate(engine, a_param_gamma, b_param_gamma); } result_type min() const { return 0.0; } result_type max() const { return 1.0; } result_type a() const { return a_gamma.alpha(); } result_type b() const { return b_gamma.alpha(); } bool operator==(const beta_distribution<result_type>& other) const { return (param() == other.param() && a_gamma == other.a_gamma && b_gamma == other.b_gamma); } bool operator!=(const beta_distribution<result_type>& other) const { return !(*this == other); } private: typedef std::gamma_distribution<result_type> gamma_dist_type; gamma_dist_type a_gamma, b_gamma; template <typename urng> result_type generate(urng& engine, gamma_dist_type& x_gamma, gamma_dist_type& y_gamma) { result_type x = x_gamma(engine); return x / (x + y_gamma(engine)); } }; template <typename chart, typename realtype> std::basic_ostream<chart>& operator<<(std::basic_ostream<chart>& os, const beta_distribution<realtype>& beta) { os << "~beta(" << beta.a() << "," << beta.b() << ")"; return os; } template <typename chart, typename realtype> std::basic_istream<chart>& operator>>(std::basic_istream<chart>& is, beta_distribution<realtype>& beta) { std::string str; realtype a, b; if (std::getline(is, str, '(') && str == "~beta" && >> && is.get() == ',' && >> b && is.get() == ')') { beta = beta_distribution<realtype>(a, b); } else { is.setstate(std::ios::failbit); } return is; } } use so:
std::random_device rd; std::mt19937 gen(rd()); sftrabbit::beta_distribution<> beta(2, 2); (int = 0; < 10000; i++) { std::cout << beta(gen) << std::endl; }
Comments
Post a Comment