digitalmars.D - Random numbers in strongly pure functions
- bearophile (35/35) Nov 22 2010 Hopefully I am not the only person that is using the "pure" attribute :-...
- Jonathan M Davis (9/10) Nov 22 2010 I try, but Phobos needs to be thoroughly "purified" before it's really g...
- Walter Bright (2/4) Nov 22 2010 I know how to solve the problem, but it's not the top priority at the mo...
- Jonathan M Davis (11/18) Nov 22 2010 Well, unfortunately, there's plenty of that to go around. Progress is be...
- =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= (14/17) Nov 24 2010 ine=20
- Jonathan M Davis (17/30) Nov 24 2010 Last time I tried outside of the 64-bit environment, it didn't work, eve...
- bearophile (4/5) Nov 22 2010 To solve it I have suggested that @optional_tag(someboolean, attribute),...
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
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
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
On Monday, November 22, 2010 15:29:20 Walter Bright wrote:Jonathan M Davis wrote: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 DavisThe 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
Jonathan M Davis wrote: It'll be nice to not need a chrooted environment to get dmdto work properly or Arch Linux (OpenSuSE manages to make it work just f=ine=20without a chrooted environemnt, but the Arch guys seem to hate proper m=ulti-lib=20for some reason...).=20OT: 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
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 =20Last 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 Davisto 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...
Nov 24 2010
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