Skip to content

Commit

Permalink
Small optimization to uniform01
Browse files Browse the repository at this point in the history
Only need to chech u < 1 if RNG returns non-integral variates.
  • Loading branch information
WebDrake committed Mar 27, 2014
1 parent d0727dc commit 93fe844
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion std/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -1594,10 +1594,24 @@ body
{
immutable T u = (rng.front - rng.min) * factor;
rng.popFront();
if (u < 1)
static if (isIntegral!R)
{
/* if RNG variates are integral, we're guaranteed
* by the definition of factor that u < 1.
*/
return u;
}
else
{
/* Otherwise we have to check, just in case a
* floating-point RNG returns a variate that is
* exactly equal to its maximum
*/
if (u < 1)
{
return u;
}
}
}

// Shouldn't ever get here.
Expand Down

0 comments on commit 93fe844

Please sign in to comment.