Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final class random generators? #9910

Open
dlangBugzillaToGithub opened this issue Sep 2, 2011 · 2 comments
Open

final class random generators? #9910

dlangBugzillaToGithub opened this issue Sep 2, 2011 · 2 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2011-09-02T02:30:49Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=6593

CC List

  • greensunny12
  • Jesse.K.Phillips+D

Description

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".
@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2011-12-05T04:09:43Z

See also issue 7067

@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 2017-07-11T18:36:29Z

Joe has done an excellent talk about this problem on DConf15: http://dconf.org/2015/talks/wakeling.html

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants