D-Programming-Language/phobos — pull request #1460 by bearophile (2013-08-09 14:54:24+00:00)
Title: Fix issue #5240 - Faster std.random.uniform() for [0.0, 1.0) range

http://d.puremagic.com/issues/show_bug.cgi?id=5240

This is my first patch.
See also: http://forum.dlang.org/thread/ic64qg$2n4t$1@digitalmars.com

An alternative design is to overload the name uniform() and when zero (or one argument for the generator) is used, to use the [0.0, 1.0] interval. But then you can't produce floats and reals.

A small benchmark:

void main() {
import std.stdio, std.datetime;
import std.random: uniform;

enum size_t N = 10_000_000;

StopWatch sw;
double tot;

tot = 0.0;
rndGen.seed(1);
sw.reset;
sw.start;
foreach (immutable _; 0 .. N) {
tot += uniform!"[]"(0.0, 1.0);
}
sw.stop;
immutable t1 = sw.peek.msecs;

tot = 0.0;
rndGen.seed(1);
sw.reset;
sw.start;
foreach (immutable _; 0 .. N) {
tot += uniform01;
}
sw.stop;
immutable t2 = sw.peek.msecs;

writeln(t1, " ", t2);
}

Compiling with -O -release -inline -noboundscheck this benchmark prints 1927 665 with dmd, and 1169 517 with ldc2.