Thread overview
[Issue 4851] New: Three suggestions for std.random
September 10, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4851

           Summary: Three suggestions for std.random
           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-09-10 15:06:37 PDT ---
It's handy to set a default random generator for randomShuffle() too, like:
void randomShuffle(Range, RandomGen = Random)(Range r, ref RandomGen gen =
rndGen);


Maybe a specified (with default) random generator is useful for randomSample()
too:
RandomSample!(R, RandomGen) randomSample(R, RandomGen = Random)(R r, size_t n,
ref RandomGen gen = rndGen, size_t total);


Very often I need to pick a single random item from a collection. In Python for
this there is the random.choice() function. I suggest to add a simple similar
function to std.random too, that is semantically similar to:
randomCover(a, rndGen).front

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



--- Comment #1 from bearophile_hugs@eml.cc 2010-09-12 18:56:32 PDT ---
A fourth possible idea:
RandomCover!(R, RandomGen) randomCover(R, RandomGen=Random)(R r, RandomGen
gen=rndGen);

-- 
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=4851


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: -------
April 16, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4851



--- Comment #2 from bearophile_hugs@eml.cc 2011-04-16 12:54:38 PDT ---
That fourth idea is also useful to avoid a little trap. This code looks
correct, here randomCover() is used to act like the Python random.choice(), but
here it keeps taking the same value:

import std.stdio, std.random;
void main() {
    // can't be const
    /*const*/ int[] data = [1, 2, 3, 4];
    foreach (i; 0 .. 20) {
        int r = randomCover(data, rndGen).front;
        write(r, " ");
    }
}

Output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


The same bug can't happen with code like this, because the random generator is not created inside the foreach scope:

import std.stdio, std.random;
void main() {
    // can't be const
    /*const*/ int[] data = [1, 2, 3, 4];
    foreach (i; 0 .. 20) {
        int r = randomCover(data).front;
        // int r = choice(data); // better
        write(r, " ");
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4851


jens.k.mueller@gmx.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.k.mueller@gmx.de


--- Comment #3 from jens.k.mueller@gmx.de 2013-02-23 05:12:55 PST ---
How much of this request is still valid?
From the documentation I find that
1. randomShuffle has default random generator
2. same for randomSample via overloads
3. choice can be expressed via
   auto choice = () => randomSample(r, 1, r.length).front;

Missing piece is randomCover with a default RandomGen.

Is this correct?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4851


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|andrei@erdani.com           |nobody@puremagic.com


--- Comment #4 from bearophile_hugs@eml.cc 2013-02-23 05:24:37 PST ---
(In reply to comment #3)

> 3. choice can be expressed via
>    auto choice = () => randomSample(r, 1, r.length).front;

That looks bad and it's error prone. It's not a replacement for choice() (and
you have missed the input argument r).


> Missing piece is randomCover with a default RandomGen.

Right.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4851



--- Comment #5 from jens.k.mueller@gmx.de 2013-02-23 05:49:23 PST ---
(In reply to comment #4)
> (In reply to comment #3)
> 
> > 3. choice can be expressed via
> >    auto choice = () => randomSample(r, 1, r.length).front;
> 
> That looks bad and it's error prone. It's not a replacement for choice() (and
> you have missed the input argument r).

Right.
Just for clarification. Adding

auto choice(R)(R r) { return randomSample(r, 1, r.length).front; };

would be fine?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4851



--- Comment #6 from bearophile_hugs@eml.cc 2013-02-23 09:45:45 PST ---
(In reply to comment #5)

> Just for clarification. Adding
> 
> auto choice(R)(R r) { return randomSample(r, 1, r.length).front; };
> 
> would be fine?

When the input is an array (Random access range) I'd like it to use a
items[uniform(0, $)]. So it's faster for the most common use case.

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