0

The below code gives me a beta distributed random variable in MSVC c++17:

std::mt19937 gen;
std::_Beta_distribution<float> beta(0.5, 0.5);
std::cout << beta(gen) << std::endl;

Unfortunately, the same code will not compile on GCC9.3.0. I need code, preferably a single approach that works across both platforms but I will do for separate approaches, to generate a Beta distributed random variable, without using Boost. How to approach this? Is std::_Beta_distribution hidden somewhere else for GCC? Or do I need to write my own code?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
willem
  • 2,617
  • 5
  • 26
  • 38
  • 2
    The naming style (`std::_Foo_bar` instead of `std::foo_bar`) hints at it being non-standard. Here's [a list](https://en.cppreference.com/w/cpp/numeric/random) of available distributions. – HolyBlackCat Sep 28 '21 at 19:10
  • Using Boost, maybe? https://stackoverflow.com/questions/4181403/generate-random-number-based-on-beta-distribution-using-boost – Bob__ Sep 28 '21 at 19:14
  • @HolyBlackCat did not know that, and I had been looking for which namespace to add. (Often things in MSVC are in std:: without adding include, but for GCC I need to include.) I saw the boost option, but would like to avoid that dependency. It seems I will need to bite the bullet and implement something like this: https://gist.github.com/sftrabbit/5068941 – willem Sep 28 '21 at 19:18
  • You could just leverage the C++-standard Gamma distribution, and use the [common mathematical trick](https://en.wikipedia.org/wiki/Beta_distribution#Computational_methods) to generate a single beta variate from two Gamma variates. See also the accepted answer for [SO-q10358064](https://stackoverflow.com/questions/10358064/random-numbers-from-beta-distribution-c). – jpmarinier Sep 29 '21 at 14:20
  • Thanks, I think HolyBlackCat's comment basically answered my question. I went with an approach similar to the one in the link you gave. – willem Sep 30 '21 at 08:51

0 Answers0