www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A strongly pure PRNG

reply "Kagamin" <spam here.lot> writes:
I believe, bearophile was interested in this - a strongly pure 
immutable Xorshift32(1) PRNG: http://dpaste.dzfl.pl/5f710daf
Jan 07 2014
next sibling parent "qznc" <qznc web.de> writes:
On Tuesday, 7 January 2014 at 12:32:23 UTC, Kagamin wrote:
 I believe, bearophile was interested in this - a strongly pure 
 immutable Xorshift32(1) PRNG: http://dpaste.dzfl.pl/5f710daf

I would consider this weakly pure, because the implicit parameter "this" is not immutable. A strongly pure PRNG cannot change internal state.
Jan 07 2014
prev sibling next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 07/01/14 13:37, qznc wrote:
 I would consider this weakly pure, because the implicit parameter "this" is not
 immutable. A strongly pure PRNG cannot change internal state.

Related -- the work I've been doing on an updated std.random: https://github.com/WebDrake/std.random2/blob/master/std/random2/generator.d It's easy to make class-based PRNGs weakly pure and const (i.e. pure in everything bar perhaps some of the seed functions, const apart from seed functions and popFront). I don't think it'd meed the criteria of a very strict functional-programming interpretation of purity, though.
Jan 07 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Kagamin:
 I believe, bearophile was interested in this

The ER: https://d.puremagic.com/issues/show_bug.cgi?id=5249 Bye, bearophile
Jan 07 2014
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 7 January 2014 at 12:37:39 UTC, qznc wrote:
 On Tuesday, 7 January 2014 at 12:32:23 UTC, Kagamin wrote:
 I believe, bearophile was interested in this - a strongly pure 
 immutable Xorshift32(1) PRNG: http://dpaste.dzfl.pl/5f710daf

I would consider this weakly pure, because the implicit parameter "this" is not immutable. A strongly pure PRNG cannot change internal state.

Without this, http://dilbert.com/strips/comic/2001-10-25/
Jan 07 2014
prev sibling next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 08/01/14 02:13, deadalnix wrote:
 Without this, http://dilbert.com/strips/comic/2001-10-25/

I guess you could do a design where you pass an immutable RNG state, and you get back whatever random number you ask for plus a new immutable state (perhaps provided via an out parameter). It's just that in this case it puts the responsibility on the user to be really careful in ensuring that the RNG state is passed around properly (the main PITA being getting back the updated state rather than passing the current one). It's really much easier to do it as a class which updates internal state, and where you can apply safe pure nothrow and (with obvious exceptions) const to just about every method; and I don't really see what you lose in a practical sense by doing it this way.
Jan 08 2014
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 8 January 2014 at 01:13:12 UTC, deadalnix wrote:
 On Tuesday, 7 January 2014 at 12:37:39 UTC, qznc wrote:
 On Tuesday, 7 January 2014 at 12:32:23 UTC, Kagamin wrote:
 I believe, bearophile was interested in this - a strongly 
 pure immutable Xorshift32(1) PRNG: 
 http://dpaste.dzfl.pl/5f710daf

I would consider this weakly pure, because the implicit parameter "this" is not immutable. A strongly pure PRNG cannot change internal state.

Without this, http://dilbert.com/strips/comic/2001-10-25/

Nice :) Of course, mandatory xkcd PRNG: https://d.puremagic.com/issues/show_bug.cgi?id=11138 You know, it *is* pure...
Jan 08 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Tuesday, 7 January 2014 at 12:37:39 UTC, qznc wrote:
 On Tuesday, 7 January 2014 at 12:32:23 UTC, Kagamin wrote:
 I believe, bearophile was interested in this - a strongly pure 
 immutable Xorshift32(1) PRNG: http://dpaste.dzfl.pl/5f710daf

I would consider this weakly pure, because the implicit parameter "this" is not immutable. A strongly pure PRNG cannot change internal state.

Formally it is immutable, see line 36.
Jan 11 2014
prev sibling next sibling parent "David Nadlinger" <code klickverbot.at> writes:
On Saturday, 11 January 2014 at 10:19:00 UTC, Kagamin wrote:
 Formally it is immutable, see line 36.

Well, by breaking the type system you can make every PRNG immutable. ;) David
Jan 11 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
Only the cast breaks the type system, I go through holes. It's 
just a joke, purity and immutability are valued for their 
deterministic traits, so a random number generator is the last 
thing one would want to make pure or immutable.
Jan 11 2014
prev sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 11/01/14 15:12, Kagamin wrote:
 Only the cast breaks the type system, I go through holes. It's just a joke,
 purity and immutability are valued for their deterministic traits, so a random
 number generator is the last thing one would want to make pure or immutable.

That depends on your concept of a PRNG. Strictly speaking it can/should consist of (i) an immutable state (ii) a pure function that takes as input the current immutable state and returns a new immutable state. In practice, of course, it's almost invariably easier to implement this as a type that has mutable internal state.
Jan 11 2014