jlb
Class JLB_Random

Object
  extended by common:Generic
      extended by jlb:JLB_Random

class 
Generic:JLB_Random

This class provides a robust pseudo-random number generator for UF.

The basic generator produces a random sequence of integers in the range from 0 to 2147483647, inclusive. Note that 2147483647 is the largest positive integer in UF.
To use, first call Init(seed) with seed an odd positive integer. This initializes the sequence. If the call to init is omitted, the default seed (1245) is used. Then i = RandomInt(0) provides successive numbers in the sequence. Also, i = RandomInt(j) with j non-zero will first reset the sequence using j as the new seed.

To produce a random integer in [1, N], inclusive, use RandomIntInRange, which uses RandomInt(). First set N with SetRange(). If the call to SetRange is omitted, the default N (16) is used.

To produce a random float in [0, 1), use RandomFloat(), which uses RandomInt(). It works by dividing the integer returned by RandomInt() by 2147483648.0 (which is a float equal to 2^31). The value 0 can be obtained, but not the value 1. This is sometimes useful. <> The random number generator is robust, probably the simplest robust generator. It uses the same algorithm as a routine in the NIST math library.
Original version: br> Blue, James L., Applied and Computational Mathematics Division, NIST
Kahaner, David K., Applied and Computational Mathematics Division, NIST
Marsaglia, George, Florida State University
Reference: Marsaglia G., "a Current View of Random Number Generators," Proceedings Computer Science and Statistics: 16th Symposium on the Interface, Elsevier, Amsterdam, 1985.


Ultra Fractal Source

Toggle UF Source Code Display

 class JLB_Random(common.ulb:Generic) {
   ; This class provides a robust pseudo-random number generator for UF. <p>
   ; The basic generator produces a random sequence of integers in the range
   ; from 0 to 2147483647, inclusive. Note that 2147483647 is the largest
   ; positive integer in UF. <br>
   ;
   ; To use, first call Init(seed) with seed an odd positive integer. This
   ; initializes the sequence. If the call to init is omitted, the default seed
   ; (1245) is used.
   ; Then i = RandomInt(0) provides successive numbers in the sequence.
   ; Also, i = RandomInt(j) with j non-zero will first reset the sequence
   ; using j as the new seed.  <p>
 
   ; To produce a random integer in [1, N], inclusive, use RandomIntInRange,
   ; which uses RandomInt(). First set N with SetRange(). If the call to
   ; SetRange is omitted, the default N (16) is used.  <p>
   ;
   ; To produce a random float in [0, 1), use RandomFloat(), which uses
   ; RandomInt(). It works by dividing the integer returned by RandomInt()
   ; by 2147483648.0 (which is a float equal to 2^31). The value
   ; 0 can be obtained, but not the value 1. This is sometimes useful.  <>
   ;
   ; The random number generator is robust, probably the simplest robust
   ; generator. It uses the same algorithm as a routine in the NIST math
   ; library.   <br>
   ;
   ; Original version: br>
   ;    Blue, James L., Applied and Computational Mathematics Division, NIST  <br>
   ;    Kahaner, David K., Applied and Computational Mathematics Division, NIST  <br>
   ;    Marsaglia, George, Florida State University   <br>
   ;
   ; Reference: Marsaglia G., "a Current View of Random Number Generators,"
   ;           Proceedings Computer Science and Statistics: 16th
   ;           Symposium on the Interface, Elsevier, Amsterdam, 1985.
   ;
 public:
   import "common.ulb"
   
   ; Constructor
   func JLB_Random(Generic pparent)
      Generic.Generic(pparent)
     ;
     ; Init with default seed.
     ;
     Init(@p_seed)
     ;
     ; Set default range maximum
     ;
     m_Nmax = 16
   endfunc
   
   ; Call to this to initialize the random number generator.
   ; @param seed an odd positive integer
   func Init(int seed)
     ; Fix up seed if Init is improperly called
     if (seed < 0)
       seed = seed + 2147483647
     endif
     if (seed % 2 == 0)
       seed = 2147483647 - seed
     endif
 
     ; Initialize the history array with a simple linear congruential generator.
     float x = seed
     int i = 0
     ; Note: floats in UF have enough range so that 9069*2147483647
     ; is exactly representable.
     repeat
       x = (9069.0 * x) % 2147483648.0
       m_hist[i] = round(x)             ; x is exactly an integer
       i = i + 1
     until (i == 17)  
     m_i =  4
     m_j = 16
   endfunc  
   
   ; @param pn If non-zero, initialize with seed = pn. If zero, just get next value
   ; @return the next value in the integer sequence. Values are from 0 to 2147483647, inclusive
   int func RandomInt(const int pn)
     
     if (pn != 0)                 ;reset seed
       Init(pn)
     endif
 
     int k = m_hist[m_i] - m_hist[m_j]
     if (k < 0)
       k = k + 2147483647
     endif
     m_hist[m_j] = k
 
     m_i = m_i - 1
     if (m_i < 0)
       m_i = 16
     endif
     
     m_j = m_j - 1
     if (m_j < 0)
       m_j = 16
     endif
     
     return k
   endfunc
   
   ; Set the maximum integer for the range version.
   ; @param Nmax maximum integer (set to 16 if Nmax <= 0)
   ; @return true if Nmax > 0, false if Nmax <= 0
   bool func SetNmax(const int Nmax)
     if (Nmax <= 0)     ; bad, so use default value
       m_Nmax = 16
       return false
     endif
     m_Nmax = Nmax
     return true
   endfunc
   
   ; Produce the next value in the sequence - range version
   ;
   ; @param pn If non-zero, initialize with seed = pn. If zero, just get next value
   ; @return the next value in the sequence. Values are from 1 to Nmax, inclusive.
   int func RandomIntInRange(const int pn)
 
     ; In general the high-order bits have better random properties than
     ; the low-order bits, so do it this way instead of RandomInt(pn)%m_Nmax.
     ;
     float x = RandomInt(pn) / 2147483648.0
     return (1 + trunc( x * m_Nmax))
   endfunc
   
   ; Produce the next value in the sequence - float version
   ;
   ; @param pn If non-zero, initialize with seed = pn. If zero, just get next value
   ; @return the next value in the sequence. Values are from 0.0 to 1.0, including 0.0 but not including 1.0.
   float func RandomFloat(const int pn)       
     return (RandomInt(pn) / 2147483648.0)
   endfunc
   
 protected:
   int m_hist[17]        ; history array
   int m_i               ; index 1 into array
   int m_j               ; index 2 into array
   int m_Nmax            ; maximum integer for range version
 
 default:
   title = "Random"
   
   int param p_seed
     caption = "Seed"
     default = 12345
     hint = "Sets the seed for the random number generator. \
     Each different seed produces a different sequence of random values."
   endparam
 }
 


Constructor Summary
JLB_Random()
           
JLB_Random(Generic pparent)
          Constructor
 
Method Summary
 void Init(int seed)
          Call to this to initialize the random number generator.
 float RandomFloat(int pn)
          Produce the next value in the sequence - float version
 int RandomInt(int pn)
           
 int RandomIntInRange(int pn)
          Produce the next value in the sequence - range version
 boolean SetNmax(int Nmax)
          Set the maximum integer for the range version.
 
Methods inherited from class common:Generic
GetParent
 
Methods inherited from class Object
 

Constructor Detail

JLB_Random

public JLB_Random(Generic pparent)
Constructor


JLB_Random

public JLB_Random()
Method Detail

Init

public void Init(int seed)
Call to this to initialize the random number generator.

Parameters:
seed - an odd positive integer

RandomInt

public int RandomInt(int pn)
Parameters:
pn - If non-zero, initialize with seed = pn. If zero, just get next value
Returns:
the next value in the integer sequence. Values are from 0 to 2147483647, inclusive

SetNmax

public boolean SetNmax(int Nmax)
Set the maximum integer for the range version.

Parameters:
Nmax - maximum integer (set to 16 if Nmax <= 0)
Returns:
true if Nmax > 0, false if Nmax <= 0

RandomIntInRange

public int RandomIntInRange(int pn)
Produce the next value in the sequence - range version

Parameters:
pn - If non-zero, initialize with seed = pn. If zero, just get next value
Returns:
the next value in the sequence. Values are from 1 to Nmax, inclusive.

RandomFloat

public float RandomFloat(int pn)
Produce the next value in the sequence - float version

Parameters:
pn - If non-zero, initialize with seed = pn. If zero, just get next value
Returns:
the next value in the sequence. Values are from 0.0 to 1.0, including 0.0 but not including 1.0.