star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit fd34d60661e37ce7613f43b73e4843cf989dbd4a
parent 276a34c5ab3e5663231e52582b71985b3e79f394
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Thu, 20 May 2021 16:38:40 +0200

Fix truncated exponential distribution

Diffstat:
Msrc/ssp_ran.c | 12++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/ssp_ran.c b/src/ssp_ran.c @@ -81,21 +81,21 @@ ssp_ran_exp_float_pdf(const float x, const float mu) double ssp_ran_exp_truncated(struct ssp_rng* rng, const double mu, const double max) { - double u, norm; + double u, r; ASSERT(rng && mu > 0 && max > 0); u = ssp_rng_canonical(rng); - norm = 1 - exp(-mu * max); - return -log(norm * u) / mu; + r = -log(1 - u * (1 - exp(-mu * max))) / mu; + return r; } float ssp_ran_exp_truncated_float(struct ssp_rng* rng, const float mu, const float max) { - float u, norm; + float u, r; ASSERT(rng && mu > 0 && max > 0); u = ssp_rng_canonical_float(rng); - norm = 1 - expf(-mu * max); - return -logf(norm * u) / mu; + r = -logf(1 - u * (1 - expf(-mu * max))) / mu; + return r; } double