Issue 6593 - final class random generators?
Summary: final class random generators?
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-02 02:30 UTC by bearophile_hugs
Modified: 2024-12-01 16:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-09-02 02:30:49 UTC
This is a _potential_ enhancement request.


A wrong D2 program:


import std.stdio, std.random;
void foo(RND)(RND rnd) {
    foreach (i; 0 .. 5)
        write(uniform(0, 10, rnd), " ");
    writeln();
}
void main() {
    auto rnd = Xorshift(1);
    foo(rnd);
    foo(rnd);
}


DMD 2.054 output:
3 1 2 7 5 
3 1 2 7 5 


The mistake is a missing ref, that causes foo to not return an updated random generator, so it always generate the same random values:
void foo(RND)(ref RND rnd) {


To avoid this bug (that I think is common enough), I suggest to experiment if it's performance-wide possibile to turn all random generators into reference things, that is final class instances, that don't require that "ref".
Comment 1 bearophile_hugs 2011-12-05 04:09:43 UTC
See also issue 7067
Comment 2 Seb 2017-07-11 18:36:29 UTC
Joe has done an excellent talk about this problem on DConf15: http://dconf.org/2015/talks/wakeling.html
Comment 3 dlangBugzillaToGithub 2024-12-01 16:14:20 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9910

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB