www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - repeating random number sequences.

reply "Jason den Dulk" <public2 jasondendulk.com> writes:
Hi

This code

   foreach (j; 0..5)
     writeln(rndGen().take(5));
   writeln(uniform(0, 1024));
   foreach (j; 0..5)
     writeln(rndGen().take(5));

produces this output

   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   813
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]

Is this correct behaviour? It appears a little inconsistant to me.

Regards
Jason
Aug 27 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 27 August 2013 at 12:59:19 UTC, Jason den Dulk wrote:
 Hi

 This code

   foreach (j; 0..5)
     writeln(rndGen().take(5));
   writeln(uniform(0, 1024));
   foreach (j; 0..5)
     writeln(rndGen().take(5));

 produces this output

   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   [3410716173, 2484862302, 280352965, 1820347603, 850366086]
   813
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]
   [2484862302, 280352965, 1820347603, 850366086, 604192828]

 Is this correct behaviour? It appears a little inconsistant to 
 me.

 Regards
 Jason
No. It is incorrect behavior. It is a *very* big problem we know about, and are trying to fix. The problem (arguably) is so big, it warrants making a brand new random2 module. Long story short: The current PRNG's a value types. This means that when you pass them around (by value) you are duplicating them. The fix (for us) is to make them all reference types, so that passign them around doesn't duplicate them. In the meantime, workarounds include: Generating a new PRGN every time you need a new sequence, or creating your own wrapper around rndGen(); struct GoodPrng { enum empty = false; void popFront() { rndGen().popFront(); } auto front() property { return rndGen().front; } } GoodPrng goodPrng() { return GoodPrng(); } This should work: void main() { foreach (j; 0..5) writeln(GoodPrng().take(5)); writeln(uniform(0, 1024)); foreach (j; 0..5) writeln(GoodPrng().take(5)); } [47855835, 916983782, 2006604655, 2074198403, 772414269] [2220086136, 3393309461, 3644080841, 1053550911, 2595436893] [3782843356, 2688374946, 3181159978, 628742771, 1672837671] [192566424, 3205182805, 521392827, 1528745543, 2713259487] [1188847012, 820882915, 1616362385, 837154982, 553045938] 400 [2350166972, 951558946, 1940218749, 1245693761, 3154088887] [4184834298, 2262977512, 3554532516, 3345988025, 1171565042] [3698692802, 538668063, 4044473111, 666762521, 865383943] [3357865623, 3653338316, 276404459, 1764534280, 1999822962] [1118055308, 3030179246, 3422085781, 4056620356, 201644357] We apologize for the inconvenience.
Aug 27 2013
prev sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 27 August 2013 at 12:59:19 UTC, Jason den Dulk wrote:
 Hi

 This code

   foreach (j; 0..5)
     writeln(rndGen().take(5));
   writeln(uniform(0, 1024));
   foreach (j; 0..5)
     writeln(rndGen().take(5));
What monarch said, though std.range.refRange() may be of interest:
Aug 28 2013
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Aug 28, 2013 at 06:18:01PM +0200, Jesse Phillips wrote:
 On Tuesday, 27 August 2013 at 12:59:19 UTC, Jason den Dulk wrote:
Hi

This code

  foreach (j; 0..5)
    writeln(rndGen().take(5));
  writeln(uniform(0, 1024));
  foreach (j; 0..5)
    writeln(rndGen().take(5));
What monarch said, though std.range.refRange() may be of interest:
That currently doesn't work too well if you use any RNG members other than the standard range API, due to: http://d.puremagic.com/issues/show_bug.cgi?id=10888 T -- Ignorance is bliss... until you suffer the consequences!
Aug 28 2013