def r8_uniform_01(): #*****************************************************************************80 # ## R8_UNIFORM_01 returns a uniform random real number in [0,1]. # # Discussion: # # This procedure returns a random floating point number from a uniform # distribution over (0,1), not including the endpoint values, using the # current random number generator. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # MATLAB version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, real R8_UNIFORM_01, a uniform random value in [0,1]. # from i4_uniform import i4_uniform from initialize import initialize from initialized_get import initialized_get # # Check whether the package must be initialized. # if (not initialized_get()): print '' print 'R8_UNIFORM_01 - Note:' print ' Initializing RNGLIB package.' initialize() # # Get a random positive integer. # i = i4_uniform() # # Scale it to a random real in [0,1]. # value = i * 4.656613057E-10 return value
def r8_uniform_01 ( ): #*****************************************************************************80 # ## R8_UNIFORM_01 returns a uniform random real number in [0,1]. # # Discussion: # # This procedure returns a random floating point number from a uniform # distribution over (0,1), not including the endpoint values, using the # current random number generator. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # MATLAB version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, real R8_UNIFORM_01, a uniform random value in [0,1]. # from i4_uniform import i4_uniform from initialize import initialize from initialized_get import initialized_get # # Check whether the package must be initialized. # if ( not initialized_get ( ) ): print '' print 'R8_UNIFORM_01 - Note:' print ' Initializing RNGLIB package.' initialize ( ) # # Get a random positive integer. # i = i4_uniform ( ) # # Scale it to a random real in [0,1]. # value = i * 4.656613057E-10 return value
def get_state(): #*****************************************************************************80 # ## GET_STATE returns the state of the current generator. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, integer CG1, CG2, the CG values for the current generator. # from cg_get import cg_get from cgn_get import cgn_get from initialize import initialize from initialized_get import initialized_get # # Check whether the package must be initialized. # if (not initialized_get()): print '' print 'GET_STATE - Note:' print ' Initializing RNGLIB package.' initialize() # # Get the current generator index. # g = cgn_get() # # Retrieve the seed values for this generator. # [cg1, cg2] = cg_get(g) return cg1, cg2
def get_state ( ): #*****************************************************************************80 # ## GET_STATE returns the state of the current generator. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, integer CG1, CG2, the CG values for the current generator. # from cg_get import cg_get from cgn_get import cgn_get from initialize import initialize from initialized_get import initialized_get # # Check whether the package must be initialized. # if ( not initialized_get ( ) ): print '' print 'GET_STATE - Note:' print ' Initializing RNGLIB package.' initialize ( ) # # Get the current generator index. # g = cgn_get ( ) # # Retrieve the seed values for this generator. # [ cg1, cg2 ] = cg_get ( g ) return cg1, cg2
def set_initial_seed ( ig1, ig2 ): #*****************************************************************************80 # ## SET_INITIAL_SEED resets the initial seed and state for all generators. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Input, integer IG1, IG2, the initial seed values # for the first generator. # 1 <= IG1 < 2147483563 # 1 <= IG2 < 2147483399 # from cgn_set import cgn_set from ig_set import ig_set from init_generator import init_generator from initialized_get import initialized_get from multmod import multmod from sys import exit a1_vw = 2082007225 a2_vw = 784306273 g_max = 32 m1 = 2147483563 m2 = 2147483399 if ( ig1 < 1 or m1 <= ig1 ): print '' print 'SET_INITIAL_SEED - Fatal error!' print ' Input parameter IG1 out of bounds.' exit ( 'SET_INITIAL_SEED - Fatal error!' ) if ( ig2 < 1 or m2 <= ig2 ): print '' print 'SET_INITIAL_SEED - Fatal error!' print ' Input parameter IG2 out of bounds.' exit ( 'SET_INITIAL_SEED - Fatal error!' ) # # Because INITIALIZE calls SET_INITIAL_SEED, it's not easy to correct # the error that arises if SET_INITIAL_SEED is called before INITIALIZE. # So don't bother trying. # if ( not initialized_get ( ) ): print '' print 'SET_INITIAL_SEED - Fatal error!' print ' The RNGLIB package has not been initialized.' exit ( 'SET_INITIAL_SEED - Fatal error!' ) # # Set the initial seed, then initialize the first generator. # g = 1 cgn_set ( g ) ig_set ( g, ig1, ig2 ) t = 0 init_generator ( t ) # # Now do similar operations for the other generators. # for g in range ( 2, g_max + 1 ): cgn_set ( g ) ig1 = multmod ( a1_vw, ig1, m1 ) ig2 = multmod ( a2_vw, ig2, m2 ) ig_set ( g, ig1, ig2 ) init_generator ( t ) # # Now choose the first generator. # g = 1 cgn_set ( g ) return
def init_generator(t): #*****************************************************************************80 # ## INIT_GENERATOR sets the current generator to initial, last or new seed. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Input, integer T, the seed type: # 0, use the seed chosen at initialization time. # 1, use the last seed. # 2, use a new seed set 2^30 values away. # from cg_set import cg_set from cgn_get import cgn_get from ig_get import ig_get from initialize import initialize from initialized_get import initialized_get from lg_get import lg_get from lg_set import lg_set from multmod import multmod a1_w = 1033780774 a2_w = 1494757890 m1 = 2147483563 m2 = 2147483399 # # Check whether the package must be initialized. # if (not initialized_get()): print '' print 'INIT_GENERATOR - Note:' print ' Initializing RNGLIB package.' initialize() # # Get the current generator index. # g = cgn_get() # # 0: Restore the initial seed. # if (t == 0): [ig1, ig2] = ig_get(g) lg1 = ig1 lg2 = ig2 lg_set(g, lg1, lg2) # # 1: Restore the last seed. # elif (t == 1): [lg1, lg2] = lg_get(g) # # Advance to a new seed. # elif (t == 2): [lg1, lg2] = lg_get(g) lg1 = multmod(a1_w, lg1, m1) lg2 = multmod(a2_w, lg2, m2) lg_set(g, lg1, lg2) else: print '' print 'INIT_GENERATOR - Fatal error!' print ' Input parameter T out of bounds.' exit('INIT_GENERATOR - Fatal error!') # # Store the new seed. # cg1 = lg1 cg2 = lg2 cg_set(g, cg1, cg2) return
def i4_uniform(): #*****************************************************************************80 # ## I4_UNIFORM generates a random positive integer. # # Discussion: # # This procedure returns a random integer following a uniform distribution # over (1, 2147483562) using the current generator. # # The original name of this function was "random()", but this conflicts # with a standard library function name in C. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, integer VALUE, the random integer. # from antithetic_get import antithetic_get from cg_get import cg_get from cg_set import cg_set from cgn_get import cgn_get from i4_division import i4_division from initialize import initialize from initialized_get import initialized_get a1 = 40014 a2 = 40692 m1 = 2147483563 m2 = 2147483399 # # Check whether the package must be initialized. # if (not initialized_get()): print '' print 'I4_UNIFORM - Note:' print ' Initializing RNGLIB package.' initialize() # # Get the current generator index. # g = cgn_get() # # Retrieve the seeds for the current generator. # [cg1, cg2] = cg_get(g) # # Update the seeds. # k = i4_division(cg1, 53668) cg1 = a1 * (cg1 - k * 53668) - k * 12211 if (cg1 < 0): cg1 = cg1 + m1 k = i4_division(cg2, 52774) cg2 = a2 * (cg2 - k * 52774) - k * 3791 if (cg2 < 0): cg2 = cg2 + m2 # # Store the updated seeds. # cg_set(g, cg1, cg2) # # Construct the random integer from the seeds. # z = cg1 - cg2 if (z < 1): z = z + m1 - 1 # # If the generator is in antithetic mode, we must reflect the value. # value = antithetic_get() if (value): z = m1 - z return z
def advance_state(k): #*****************************************************************************80 # ## ADVANCE_STATE advances the state of the current generator. # # Discussion: # # This procedure advances the state of the current generator by 2^K # values and resets the initial seed to that value. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Input, integer K, indicates that the generator is to be # advanced by 2^K values. # 0 <= K. # from cg_get import cg_get from cg_set import cg_set from cgn_get import cgn_get from initialize import initialize from initialized_get import initialized_get from multmod import multmod from sys import exit a1 = 40014 a2 = 40692 m1 = 2147483563 m2 = 2147483399 if (k < 0): print '' print 'ADVANCE_STATE - Fatal error!' print ' Input exponent K is out of bounds.' exit('ADVANCE_STATE - Fatal error!') # # Check whether the package must be initialized. # if (not initialized_get()): print '' print 'ADVANCE_STATE - Note:' print ' Initializing RNGLIB package.' initialize() # # Get the current generator index. # g = cgn_get() b1 = a1 b2 = a2 for i in range(1, k + 1): b1 = multmod(b1, b1, m1) b2 = multmod(b2, b2, m2) [cg1, cg2] = cg_get(g) cg1 = multmod(b1, cg1, m1) cg2 = multmod(b2, cg2, m2) cg_set(g, cg1, cg2) return
def advance_state ( k ): #*****************************************************************************80 # ## ADVANCE_STATE advances the state of the current generator. # # Discussion: # # This procedure advances the state of the current generator by 2^K # values and resets the initial seed to that value. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Input, integer K, indicates that the generator is to be # advanced by 2^K values. # 0 <= K. # from cg_get import cg_get from cg_set import cg_set from cgn_get import cgn_get from initialize import initialize from initialized_get import initialized_get from multmod import multmod from sys import exit a1 = 40014 a2 = 40692 m1 = 2147483563 m2 = 2147483399 if ( k < 0 ): print '' print 'ADVANCE_STATE - Fatal error!' print ' Input exponent K is out of bounds.' exit ( 'ADVANCE_STATE - Fatal error!' ) # # Check whether the package must be initialized. # if ( not initialized_get ( ) ): print '' print 'ADVANCE_STATE - Note:' print ' Initializing RNGLIB package.' initialize ( ) # # Get the current generator index. # g = cgn_get ( ) b1 = a1 b2 = a2 for i in range ( 1, k + 1 ): b1 = multmod ( b1, b1, m1 ) b2 = multmod ( b2, b2, m2 ) [ cg1, cg2 ] = cg_get ( g ) cg1 = multmod ( b1, cg1, m1 ) cg2 = multmod ( b2, cg2, m2 ) cg_set ( g, cg1, cg2 ) return
def i4_uniform ( ): #*****************************************************************************80 # ## I4_UNIFORM generates a random positive integer. # # Discussion: # # This procedure returns a random integer following a uniform distribution # over (1, 2147483562) using the current generator. # # The original name of this function was "random()", but this conflicts # with a standard library function name in C. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # Original Pascal version by Pierre L'Ecuyer, Serge Cote. # PYTHON version by John Burkardt. # # Reference: # # Pierre LEcuyer, Serge Cote, # Implementing a Random Number Package with Splitting Facilities, # ACM Transactions on Mathematical Software, # Volume 17, Number 1, March 1991, pages 98-111. # # Parameters: # # Output, integer VALUE, the random integer. # from antithetic_get import antithetic_get from cg_get import cg_get from cg_set import cg_set from cgn_get import cgn_get from i4_division import i4_division from initialize import initialize from initialized_get import initialized_get a1 = 40014 a2 = 40692 m1 = 2147483563 m2 = 2147483399 # # Check whether the package must be initialized. # if ( not initialized_get ( ) ): print '' print 'I4_UNIFORM - Note:' print ' Initializing RNGLIB package.' initialize ( ) # # Get the current generator index. # g = cgn_get ( ) # # Retrieve the seeds for the current generator. # [ cg1, cg2 ] = cg_get ( g ) # # Update the seeds. # k = i4_division ( cg1, 53668 ) cg1 = a1 * ( cg1 - k * 53668 ) - k * 12211 if ( cg1 < 0 ): cg1 = cg1 + m1 k = i4_division ( cg2, 52774 ) cg2 = a2 * ( cg2 - k * 52774 ) - k * 3791 if ( cg2 < 0 ): cg2 = cg2 + m2 # # Store the updated seeds. # cg_set ( g, cg1, cg2 ) # # Construct the random integer from the seeds. # z = cg1 - cg2 if ( z < 1 ): z = z + m1 - 1 # # If the generator is in antithetic mode, we must reflect the value. # value = antithetic_get ( ) if ( value ): z = m1 - z return z