Correctly round towards zero in random(uint32_t)

This commit is contained in:
kuepper 2007-08-21 14:47:14 +00:00
commit ac601df371

View file

@ -178,7 +178,10 @@ public :
*/ */
uint32_t random(uint32_t m) 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 /** Biased coin toss