Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Last active February 27, 2023 21:23
Show Gist options
  • Select an option

  • Save Xenakios/09fe35f995354a2ac194889ced77c1ea to your computer and use it in GitHub Desktop.

Select an option

Save Xenakios/09fe35f995354a2ac194889ced77c1ea to your computer and use it in GitHub Desktop.
template<typename RNG=std::default_random_engine, typename ResultType=float>
struct RandomDistributionGenerator
{
RNG rng;
std::uniform_real_distribution<ResultType> distrib_uniform;
std::normal_distribution<ResultType> distrib_normal;
std::exponential_distribution<ResultType> distrib_expo;
std::cauchy_distribution<ResultType> distrib_cauchy;
RandomDistributionGenerator() {}
RandomDistributionGenerator(unsigned int seed) : rng(seed) {}
void setSeed(unsigned int seed) { rng.seed(seed); }
void setUniformDistributionParameters(ResultType low, ResultType high)
{
distrib_uniform = std::uniform_real_distribution<ResultType>(low,high);
}
void setNormalDistributionParameters(ResultType mean, ResultType deviation)
{
distrib_normal = std::normal_distribution<ResultType>(mean,deviation);
}
void setExponentialDistributionParameter(ResultType lambda)
{
distrib_expo = std::exponential_distribution<ResultType>(lambda);
}
void setCauchyDistributionParameters(ResultType quasimean, ResultType deviation)
{
distrib_cauchy = std::cauchy_distribution<ResultType>(quasimean,deviaton);
}
ResultType nextUniform() { return distrib_uniform(rng); }
ResultType nextNormal() { return distrib_normal(rng); }
ResultType nextExponential() { return distrib_expo(rng); }
ResultType nextCauchy() { return distrib_cauchy(rng); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment