Bug 4851 – Three suggestions for std.random

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-09-10T15:06:00Z
Last change time
2017-07-06T13:15:06Z
Keywords
bootcamp, pull
Assigned to
edi33416
Creator
bearophile_hugs

Comments

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?
Comment #9 by edi33416 — 2016-11-11T16:22:11Z
I have created a PR that adds the choice function to std.random. The link is below: https://github.com/dlang/phobos/pull/4897
Comment #10 by github-bugzilla — 2016-11-15T15:31:10Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/783d15bfa0e55d753999e7cc38236763c6b95092 enhancement issue 4851: add choice function to std.random https://github.com/dlang/phobos/commit/adb71a6c6b8e51680ce2337eb93c28fa7ea9a3fd enhancement issue 4851: use assert in choice function https://github.com/dlang/phobos/commit/6bc3a4f343ab653e85d11966a66664b559da59c8 Merge pull request #4897 from edi33416/implement_choice enhancement issue 4851: add choice function to std.random
Comment #11 by github-bugzilla — 2016-12-27T13:11:21Z
Commits pushed to scope at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/783d15bfa0e55d753999e7cc38236763c6b95092 enhancement issue 4851: add choice function to std.random https://github.com/dlang/phobos/commit/adb71a6c6b8e51680ce2337eb93c28fa7ea9a3fd enhancement issue 4851: use assert in choice function https://github.com/dlang/phobos/commit/6bc3a4f343ab653e85d11966a66664b559da59c8 Merge pull request #4897 from edi33416/implement_choice
Comment #12 by github-bugzilla — 2017-01-07T03:02:35Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/783d15bfa0e55d753999e7cc38236763c6b95092 enhancement issue 4851: add choice function to std.random https://github.com/dlang/phobos/commit/adb71a6c6b8e51680ce2337eb93c28fa7ea9a3fd enhancement issue 4851: use assert in choice function https://github.com/dlang/phobos/commit/6bc3a4f343ab653e85d11966a66664b559da59c8 Merge pull request #4897 from edi33416/implement_choice
Comment #13 by github-bugzilla — 2017-01-16T23:24:50Z
Commits pushed to newCTFE at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/783d15bfa0e55d753999e7cc38236763c6b95092 enhancement issue 4851: add choice function to std.random https://github.com/dlang/phobos/commit/adb71a6c6b8e51680ce2337eb93c28fa7ea9a3fd enhancement issue 4851: use assert in choice function https://github.com/dlang/phobos/commit/6bc3a4f343ab653e85d11966a66664b559da59c8 Merge pull request #4897 from edi33416/implement_choice