Mastering E-Prime: PRNG vs. standard randomization functions

David McFarlane mcfarla9 at msu.edu
Fri Aug 26 15:36:19 UTC 2011


E-Basic offers two sets of randomization functions:  The standard set 
from VBA (Randomize(), Rnd(), Random()), and the PRNG object (with 
methods GetSeed, SetSeed, Reset, GetNext, GetPrev, Random()).  How do 
these differ?  Do they use the same underlying pseudo-random number 
generator?  Why use one rather than the other?

As demonstrated by Paul Groot at 
http://groups.google.com/group/e-prime/browse_thread/thread/ccda3fe57a595c54 
, both sets of functions *do* use the same underlying pseudo-random 
number generator (but you should still test that out for yourself!), 
and provide equivalent results.  So what's the point?  Well, the 
standard VBA set does have a confusing interface.  I posit that 
E-Basic provides the PRNG object simply to gather all these functions 
into one tidy E-Object, and along the way provides one extra function 
lacking from the standard set.  E-Basic still  includes the standard 
set for those VBA programmers who prefer it, as well as to allow 
legacy code to work.

Some further notes (in the following, x indicates an arbitrary 
number, +x a number greater than 0, -x a number less than 0):

- PRNG.GetSeed
       has no equivalent in the standard VBA set.

- PRNG.SetSeed x
       does the same as
   Randomize x
       (note that "Randomize" without an argument uses the system clock
       for the seed; PRNG has no simple equivalent to this --
       "PRNG.SetSeed" without an argument sets the seed to 1).

- PRNG.Reset
       is a synonym for
   PRNG.SetSeed 0
       or
   Randomize 0

- PRNG.GetNext
       does the same as
   Rnd(+x)

- PRNG.GetPrev
       does the same as
   Rnd(0)
       i.e., they simply repeat the value of the latest random number
       generated (e.g., from the latest use of PRNG.GetNext or Rnd(+x)).
       (Note also that after PRNG.Random() or Random() it returns the
       full double value, not the truncated long integer value.)

- PRNG.Random(min,max)
       does the same as
   Random(min,max)
       0 <= min < max, or they produce the run time error "Invalid Range
       Specified".

- Rnd(-x)
       returns the same as
   Randomize Abs(x)  ' (same x as above)
   Rnd(+y)
       which in turn does the same as
   PRNG.SetSeed Abs(x)
   PRNG.GetNext

So which should you use?  That comes down to your personal 
programming taste or style:  Would you rather use well-known standard 
VBA statements & functions, or keep things organized as methods from 
one E-Object?  Whichever you choose, you might help others by 
explaining your choice with comments in your program.

-- David McFarlane, Professional Faultfinder

-- 
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To post to this group, send email to e-prime at googlegroups.com.
To unsubscribe from this group, send email to e-prime+unsubscribe at googlegroups.com.
For more options, visit this group at http://groups.google.com/group/e-prime?hl=en.



More information about the Eprime mailing list