Thread overview
[Issue 5240] New: Faster std.random.uniform() for [0.0, 1.0) range
Nov 21, 2010
Denis Derman
Nov 21, 2010
strtr@despam.it
Nov 22, 2010
strtr@despam.it
November 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240

           Summary: Faster std.random.uniform() for [0.0, 1.0) range
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-11-19 07:16:51 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240



--- Comment #1 from bearophile_hugs@eml.cc 2010-11-19 12:10:42 PST ---
A specialized overload/function like this is useful in Phobos even if it's not faster.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240


Denis Derman <denis.spir@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denis.spir@gmail.com


--- Comment #2 from Denis Derman <denis.spir@gmail.com> 2010-11-21 00:55:34 PST ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240


strtr@despam.it changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |strtr@despam.it


--- Comment #3 from strtr@despam.it 2010-11-21 05:17:43 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240



--- Comment #4 from bearophile_hugs@eml.cc 2010-11-22 09:44:33 PST ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240



--- Comment #5 from strtr@despam.it 2010-11-22 10:19:33 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5240



--- Comment #6 from bearophile_hugs@eml.cc 2010-11-22 12:05:44 PST ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5240


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------