www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Random numbers in strongly pure functions

reply bearophile <bearophileHUGS lycos.com> writes:
Hopefully I am not the only person that is using the "pure" attribute :-)

I have felt the need to generate random numbers in pure functions too, so I
have suggested to add to the std.random module a strongly pure function that
takes a seed (and other arguments) and returns the next seed (and optionally a
ranged value too).

An example:


import std.stdio: writeln;

immutable struct rndPair {
    double seed, rnd;
}

// strongly pure
// Probably with DMD 2.050 a std.typecons.Tuple can't
// be used as return value here
pure nothrow rndPair nextRandom(const double seed, const double max) {
    enum int IA = 3_877, IC = 29_573, IM = 139_968;
    immutable double new_seed = (seed * IA + IC) % IM;
    return rndPair(new_seed, max * (new_seed * (1.0 / IM)));
}

// strongly pure
pure double[] foo(const int n, const double firstSeed=42) {
    double seed = firstSeed;
    auto res = new double[n];
    foreach (ref r; res) {
        auto seed_rnd = nextRandom(seed, 1.0);
        r = seed_rnd.rnd;
        seed = seed_rnd.seed;
    }
    return res;
}

void main() {
    writeln(foo(5));
    // Output:
    // [0.37465, 0.729024, 0.636467, 0.793481, 0.538545]
}


For a better example and more info click here:
http://d.puremagic.com/issues/show_bug.cgi?id=5249

Bye,
bearophile
Nov 22 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, November 22, 2010 04:36:46 bearophile wrote:
 Hopefully I am not the only person that is using the "pure" attribute :-)
I try, but Phobos needs to be thoroughly "purified" before it's really going to work. And at the moment, unless you go out of your way and declare multiple versions of template functions which are pure or impure based on their template arguments, many functions which _should_ be pure _can't_ be. So, much as I try to use purity, even with the new weak purity rules, it's still pretty hard to use it much. The situtation should improve, but it wouldn't surprise me if a lot of people pretty much ignored pure at the moment due to problems using it. - Jonathan M Davis
Nov 22 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
Jonathan M Davis wrote:
 The situtation should improve, but it wouldn't surprise me if a lot 
 of people pretty much ignored pure at the moment due to problems using it.
I know how to solve the problem, but it's not the top priority at the moment.
Nov 22 2010
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, November 22, 2010 15:29:20 Walter Bright wrote:
 Jonathan M Davis wrote:
 The situtation should improve, but it wouldn't surprise me if a lot
 of people pretty much ignored pure at the moment due to problems using
 it.
I know how to solve the problem, but it's not the top priority at the moment.
Well, unfortunately, there's plenty of that to go around. Progress is being made though, so that's good. And as far as pure goes, adding weak purity definitely was a big step forward in useability, so while pure definitely has big issues, it's closer to being properly useable. Regardless, I'm definitely looking forward to the 64-bit port. It'll be nice to not need a chrooted environment to get dmd to work properly or Arch Linux (OpenSuSE manages to make it work just fine without a chrooted environemnt, but the Arch guys seem to hate proper multi-lib for some reason...). Your work is definitely appreciated, even if there's plenty more of it which would be nice to have done but isn't yet. - Jonathan M Davis
Nov 22 2010
parent reply =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Jonathan M Davis wrote:
It'll be nice to not need a chrooted environment to get dmd
 to work properly or Arch Linux (OpenSuSE manages to make it work just f=
ine=20
 without a chrooted environemnt, but the Arch guys seem to hate proper m=
ulti-lib=20
 for some reason...).=20
OT: Arch switched to a true multi-lib setup 4 months ago: http://www.archlinux.org/news/true-multilib-for-arch-linux-x86_64/ Or is there something I missed? Note: I've been able to run DMD on 64bits Arch Linux without problems... Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Nov 24 2010
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 24, 2010 10:26:50 J=E9r=F4me M. Berger wrote:
 Jonathan M Davis wrote:
 It'll be nice to not need a chrooted environment to get dmd
=20
 to work properly or Arch Linux (OpenSuSE manages to make it work just
 fine without a chrooted environemnt, but the Arch guys seem to hate
 proper multi-lib for some reason...).
=20 OT: Arch switched to a true multi-lib setup 4 months ago: http://www.archlinux.org/news/true-multilib-for-arch-linux-x86_64/ Or is there something I missed? =20 Note: I've been able to run DMD on 64bits Arch Linux without problems...
Last time I tried outside of the 64-bit environment, it didn't work, even w= ith=20 the new multilib stuff (which I did not get the impression was full multili= b yet,=20 but I haven't dug into it thoroughly). One of the main problems that I've h= ad=20 using dmd in straight up 64-bit land though is that I like to fully statica= lly=20 link my binaries, and -L-lstatic doesn't work with dmd for some reason:=20 http://d.puremagic.com/issues/show_bug.cgi?id=3D4376 And in Arch, their gcc setup is still too 64-bit-centric to link 32 bit cod= e (or=20 at least, I couldn't get it to do it). I've been able to it in OpenSuSE how= ever.=20 Regardless, the situation will be simplified once 64-bit dmd is ready. =2D Jonathan M Davis
Nov 24 2010
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Walter:

 I know how to solve the problem, but it's not the top priority at the moment.
To solve it I have suggested that optional_tag(someboolean, attribute), but what is your idea? I am curious :-) Bye, bearophile
Nov 22 2010