www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4851] New: Three suggestions for std.random

reply d-bugmail puremagic.com writes:
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



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: -------
Sep 10 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4851




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: -------
Sep 12 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jan 09 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4851




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: -------
Apr 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Feb 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
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





 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: -------
Feb 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4851






 
 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: -------
Feb 23 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4851






 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: -------
Feb 23 2013