c – 具有Beta分布的随机数发生器

前端之家收集整理的这篇文章主要介绍了c – 具有Beta分布的随机数发生器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要像betarand(a,b)这样的函数的c或c源代码,它产生带有beta分布的随机数.我知道我可以使用boost库但是我要将它移植到CUDA架构中,所以我需要代码.有人能帮助我吗?
同时我有betapdf(Beta概率密度函数).但我不知道如何使用它来创建随机数:).

解决方法

C 11随机数库不提供beta分布.但是,beta分布可以根据库提供的两个gamma分布来建模.我已经为你实现了一个 beta_distribution的std :: gamma_distribution.据我所知,它完全符合随机数分布的要求.
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <random>
  5.  
  6. namespace sftrabbit {
  7.  
  8. template <typename RealType = double>
  9. class beta_distribution
  10. {
  11. public:
  12. typedef RealType result_type;
  13.  
  14. class param_type
  15. {
  16. public:
  17. typedef beta_distribution distribution_type;
  18.  
  19. explicit param_type(RealType a = 2.0,RealType b = 2.0)
  20. : a_param(a),b_param(b) { }
  21.  
  22. RealType a() const { return a_param; }
  23. RealType b() const { return b_param; }
  24.  
  25. bool operator==(const param_type& other) const
  26. {
  27. return (a_param == other.a_param &&
  28. b_param == other.b_param);
  29. }
  30.  
  31. bool operator!=(const param_type& other) const
  32. {
  33. return !(*this == other);
  34. }
  35.  
  36. private:
  37. RealType a_param,b_param;
  38. };
  39.  
  40. explicit beta_distribution(RealType a = 2.0,RealType b = 2.0)
  41. : a_gamma(a),b_gamma(b) { }
  42. explicit beta_distribution(const param_type& param)
  43. : a_gamma(param.a()),b_gamma(param.b()) { }
  44.  
  45. void reset() { }
  46.  
  47. param_type param() const
  48. {
  49. return param_type(a(),b());
  50. }
  51.  
  52. void param(const param_type& param)
  53. {
  54. a_gamma = gamma_dist_type(param.a());
  55. b_gamma = gamma_dist_type(param.b());
  56. }
  57.  
  58. template <typename URNG>
  59. result_type operator()(URNG& engine)
  60. {
  61. return generate(engine,a_gamma,b_gamma);
  62. }
  63.  
  64. template <typename URNG>
  65. result_type operator()(URNG& engine,const param_type& param)
  66. {
  67. gamma_dist_type a_param_gamma(param.a()),b_param_gamma(param.b());
  68. return generate(engine,a_param_gamma,b_param_gamma);
  69. }
  70.  
  71. result_type min() const { return 0.0; }
  72. result_type max() const { return 1.0; }
  73.  
  74. result_type a() const { return a_gamma.alpha(); }
  75. result_type b() const { return b_gamma.alpha(); }
  76.  
  77. bool operator==(const beta_distribution<result_type>& other) const
  78. {
  79. return (param() == other.param() &&
  80. a_gamma == other.a_gamma &&
  81. b_gamma == other.b_gamma);
  82. }
  83.  
  84. bool operator!=(const beta_distribution<result_type>& other) const
  85. {
  86. return !(*this == other);
  87. }
  88.  
  89. private:
  90. typedef std::gamma_distribution<result_type> gamma_dist_type;
  91.  
  92. gamma_dist_type a_gamma,b_gamma;
  93.  
  94. template <typename URNG>
  95. result_type generate(URNG& engine,gamma_dist_type& x_gamma,gamma_dist_type& y_gamma)
  96. {
  97. result_type x = x_gamma(engine);
  98. return x / (x + y_gamma(engine));
  99. }
  100. };
  101.  
  102. template <typename CharT,typename RealType>
  103. std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os,const beta_distribution<RealType>& beta)
  104. {
  105. os << "~Beta(" << beta.a() << "," << beta.b() << ")";
  106. return os;
  107. }
  108.  
  109. template <typename CharT,typename RealType>
  110. std::basic_istream<CharT>& operator>>(std::basic_istream<CharT>& is,beta_distribution<RealType>& beta)
  111. {
  112. std::string str;
  113. RealType a,b;
  114. if (std::getline(is,str,'(') && str == "~Beta" &&
  115. is >> a && is.get() == ',' && is >> b && is.get() == ')') {
  116. beta = beta_distribution<RealType>(a,b);
  117. } else {
  118. is.setstate(std::ios::failbit);
  119. }
  120. return is;
  121. }
  122.  
  123. }

像这样使用它:

  1. std::random_device rd;
  2. std::mt19937 gen(rd());
  3. sftrabbit::beta_distribution<> beta(2,2);
  4. for (int i = 0; i < 10000; i++) {
  5. std::cout << beta(gen) << std::endl;
  6. }

猜你在找的C&C++相关文章