From ac601df3716ff4a00442631851fa644f4d461f96 Mon Sep 17 00:00:00 2001 From: kuepper Date: Tue, 21 Aug 2007 14:47:14 +0000 Subject: [PATCH] Correctly round towards zero in random(uint32_t) --- eo/src/utils/eoRNG.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/eo/src/utils/eoRNG.h b/eo/src/utils/eoRNG.h index 69a6c809..d2857a83 100644 --- a/eo/src/utils/eoRNG.h +++ b/eo/src/utils/eoRNG.h @@ -178,7 +178,10 @@ public : */ uint32_t random(uint32_t m) { - return uint32_t(uniform() * double(m)); + // Make sure we always round towards zero, in order to get the + // half-open interval we want (as documented). The floor function + // does exactly this for the (always positive) values occuring here. + return uint32_t(floor(uniform() * double(m))); } /** Biased coin toss