Bug 5240 – Faster std.random.uniform() for [0.0, 1.0) range

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-19T07:16:00Z
Last change time
2014-04-03T04:37:26Z
Assigned to
andrei
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-11-19T07:16:51Z
std.random.uniform() is a powerful and flexible function, but some tests show that it's slow. It also calls nextafter() that is not fast. A very common use case is the generation of random doubles in the [0.0, 0.1[ range (open on the right). So I suggest to speed up this special case. This special case is present in the Python standard module "random" too, it is just named random(). A way to support it is just to add a uniform() that takes no arguments (and hopefully it doesn't need to call nextafter() inside): import std.random: uniform; void main() { double d = uniform(); // equivalent to uniform(0.0, 1.0) but faster } This function that takes no arguments may also have a name like "uniform01()" or "random()". Another special case that may be worth supporting is the generation of a random size_t. This is useful because it avoids using floating point values at all, and probably minimizes other operations too to generate it, so it may be faster still than the uniform() with no arguments.
Comment #1 by bearophile_hugs — 2010-11-19T12:10:42Z
A specialized overload/function like this is useful in Phobos even if it's not faster.
Comment #2 by denis.spir — 2010-11-21T00:55:34Z
I would vote for uniform01(). Having it called random() would implicitely promote to the default random function, from which all other forms of random would/should be computed. Denis
Comment #3 by strtr — 2010-11-21T05:17:43Z
I am probably missing something, but what is wrong with uniform!(0,1) ? Then you can add optimizations for all the special cases you want.
Comment #4 by bearophile_hugs — 2010-11-22T09:44:33Z
(In reply to comment #3) > I am probably missing something, but what is wrong with uniform!(0,1) ? > Then you can add optimizations for all the special cases you want. Currently uniform() uses run-time arguments, not template ones. -------------------------------------- See also: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=122634
Comment #5 by strtr — 2010-11-22T10:19:33Z
I was commenting on the suggested name uniform01. Templating the function seemed obvious to me and I was also surprised you didn't suggest doing that so I thought I was missing something there. As for speed, as tn mentioned on the newsgroup, subnormal numbers considerately slow down calculations.
Comment #6 by bearophile_hugs — 2010-11-22T12:05:44Z
(In reply to comment #5) > I was commenting on the suggested name uniform01. > Templating the function seemed obvious to me and I was also surprised you > didn't suggest doing that so I thought I was missing something there. I don't like the idea of a templated ranged uniform. In most cases is not what I need or what's better. > As for speed, as tn mentioned on the newsgroup, subnormal numbers considerately > slow down calculations. It's in the link of Comment 4
Comment #7 by github-bugzilla — 2014-04-02T23:34:23Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/d0727dc7a02a33d855cc141f09f883501077797b Fix Issue #5240: faster std.random.uniform for [0.0, 1.0) range Following typical design in other languages, the function is named uniform01. It can be templated on floating-point type, defaulting to double if none is specified. The implementation is pretty much derived from its counterpart in Boost.Random, in particular in its taking account of the different requirements for integral versus floating-point RNG values, and in checking that the generated variate is less than 1 before returning. This last check is necessary simply because we can't guarantee, in the case of a floating-point based RNG, that we will not get a result exactly equal to 1. Fixes http://d.puremagic.com/issues/show_bug.cgi?id=5240 https://github.com/D-Programming-Language/phobos/commit/0ba2004bd4d725c5ccff227a65bc19a6e3371381 Merge pull request #2050 from WebDrake/uniform01 Fix Issue #5240: faster std.random.uniform for [0.0, 1.0) range