Comment #0 by bearophile_hugs — 2010-09-10T15:06:37Z
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
Comment #1 by bearophile_hugs — 2010-09-12T18:56:32Z
A fourth possible idea:
RandomCover!(R, RandomGen) randomCover(R, RandomGen=Random)(R r, RandomGen gen=rndGen);
Comment #2 by bearophile_hugs — 2011-04-16T12:54:38Z
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, " ");
}
}
Comment #3 by jens.k.mueller — 2013-02-23T05:12:55Z
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?
Comment #4 by bearophile_hugs — 2013-02-23T05:24:37Z
(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.
Comment #5 by jens.k.mueller — 2013-02-23T05:49:23Z
(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?
Comment #6 by bearophile_hugs — 2013-02-23T09:45:45Z
(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.
Comment #7 by bearophile_hugs — 2014-03-29T16:13:21Z
If choice returns a reference you can do:
void knuthShuffle(T)(T[] r) {
foreach_reverse (immutable i, ref ri; r[1 .. $ - 1])
r[0 .. i + 1].choice.swap(ri);
}
Comment #8 by andrei — 2016-10-14T12:46:22Z
Adding some of my own:
* uniform!int should give a uniform, full-range int. Same of course for all integral types.
* uniform!bool should give a random bit (economically, i.e. generate a new 64-bit number every 64 calls)
* uniform!double should give a double in the range [0.0, 1.0). Or is [0.0, 1.0] more appropriate?
* uniform!(int[])(30) should return an array of 30 integers
* uniform!string(30) should return a uniform string of 30... bytes or code points?