digitalmars.D.bugs - [Issue 4851] New: Three suggestions for std.random
- d-bugmail puremagic.com (27/27) Sep 10 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (8/8) Sep 12 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (10/10) Jan 09 2011 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (31/31) Apr 16 2011 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (17/17) Feb 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (13/16) Feb 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (10/17) Feb 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=4851
- d-bugmail puremagic.com (8/13) Feb 23 2013 http://d.puremagic.com/issues/show_bug.cgi?id=4851
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
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
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
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
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
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
http://d.puremagic.com/issues/show_bug.cgi?id=4851Right. 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: -------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).
Feb 23 2013
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









d-bugmail puremagic.com 