def rnglib_test02(): #*****************************************************************************80 # ## RNGLIB_TEST02 calls R4_UNIFORM_01 10 times, just to show how it is done. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST02' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize() # # Set the current generator index to #2. # g = 2 cgn_set(g) print '' print ' Current generator index = %d' % (g) # # Repeatedly call R4_UNIFORM_01(). # print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range(1, 11): u = r4_uniform_01() print ' %2d %14.6g' % (i, u)
def rnglib_test02 ( ): #*****************************************************************************80 # ## RNGLIB_TEST02 calls R4_UNIFORM_01 10 times, just to show how it is done. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST02' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize ( ) # # Set the current generator index to #2. # g = 2 cgn_set ( g ) print '' print ' Current generator index = %d' % ( g ) # # Repeatedly call R4_UNIFORM_01(). # print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range ( 1, 11 ): u = r4_uniform_01 ( ) print ' %2d %14.6g' % ( i, u )
def rnglib_test01(): #*****************************************************************************80 # ## RNGLIB_TEST01 calls I4_UNIFORM 10 times, just to show how it is done. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from i4_uniform import i4_uniform from initialize import initialize print '' print 'RNGLIB_TEST01' print ' I4_UNIFORM ( ) returns a random positive integer' print ' using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize() # # Set the current generator index to #1. # g = 1 cgn_set(g) print '' print ' Current generator index = %d' % (g) # # Now call I4_UNIFORM(). # print '' print ' I I4_UNIFORM ( )' print '' for i in range(1, 11): j = i4_uniform() print ' %2d %12d' % (i, j)
def initialize(): #*****************************************************************************80 # ## INITIALIZE initializes the random number generator library. # # 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: # # None # from antithetic_set import antithetic_set from cgn_set import cgn_set from initialized_set import initialized_set from set_initial_seed import set_initial_seed g_max = 32 # # Remember that we have called INITIALIZE(). # initialized_set() # # Initialize all generators to have FALSE antithetic value. # value = False for g in range(1, g_max + 1): cgn_set(g) antithetic_set(value) # # Set the initial seeds. # ig1 = 1234567890 ig2 = 123456789 set_initial_seed(ig1, ig2) # # Initialize the current generator index to 1. # g = 1 cgn_set(g) print '' print 'INITIALIZE - Note:' print ' The RNGLIB package has been initialized.' return
def rnglib_test04(): #*****************************************************************************80 # ## RNGLIB_TEST04 demonstrates the use of multiple streams. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from init_generator import init_generator from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST04' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize() print '' print ' Let us call generators #3, #6 and #9.' # # Use three separate generators, 3, 6 and 9. # Force them to start at their initial seeds. # g = [3, 6, 9] print '' for j in range(0, 3): print ' Initialize generator %d' % (g[j]) cgn_set(g[j]) init_generator(0) # # Call the generators in the order 3, 6, 9. # print '' print ' I R4_UNIFORM_01 ( 3 ) R4_UNIFORM_01 ( 6 ) R4_UNIFORM_01 ( 9 )' print '' for i in range(1, 11): print ' %2d' % (i), for j in range(0, 3): cgn_set(g[j]) u = r4_uniform_01() print ' %14.6g' % (u), print '' # # Restart the generators at their initial seeds. # g = [6, 9, 3] print '' for j in range(0, 3): print ' Reinitialize generator %d' % (g[j]) cgn_set(g[j]) init_generator(0) # # Call them in a different order, same result. # print '' print ' I R4_UNIFORM_01 ( 6 ) R4_UNIFORM_01 ( 9 ) R4_UNIFORM_01 ( 3 )' print '' for i in range(1, 11): print ' %2d' % (i), for j in range(0, 3): cgn_set(g[j]) u = r4_uniform_01() print ' %14.6g' % (u), print ''
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 rnglib_test04 ( ): #*****************************************************************************80 # ## RNGLIB_TEST04 demonstrates the use of multiple streams. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from init_generator import init_generator from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST04' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize ( ) print '' print ' Let us call generators #3, #6 and #9.' # # Use three separate generators, 3, 6 and 9. # Force them to start at their initial seeds. # g = [ 3, 6, 9 ] print '' for j in range ( 0, 3 ): print ' Initialize generator %d' % ( g[j] ) cgn_set ( g[j] ) init_generator ( 0 ) # # Call the generators in the order 3, 6, 9. # print '' print ' I R4_UNIFORM_01 ( 3 ) R4_UNIFORM_01 ( 6 ) R4_UNIFORM_01 ( 9 )' print '' for i in range ( 1, 11 ): print ' %2d' % ( i ), for j in range ( 0, 3 ): cgn_set ( g[j] ) u = r4_uniform_01 ( ) print ' %14.6g' % ( u ), print '' # # Restart the generators at their initial seeds. # g = [ 6, 9, 3 ] print '' for j in range ( 0, 3 ): print ' Reinitialize generator %d' % ( g[j] ) cgn_set ( g[j] ) init_generator ( 0 ) # # Call them in a different order, same result. # print '' print ' I R4_UNIFORM_01 ( 6 ) R4_UNIFORM_01 ( 9 ) R4_UNIFORM_01 ( 3 )' print '' for i in range ( 1, 11 ): print ' %2d' % ( i ), for j in range ( 0, 3 ): cgn_set ( g[j] ) u = r4_uniform_01 ( ) print ' %14.6g' % ( u ), print ''
def rnglib_test03(): #*****************************************************************************80 # ## RNGLIB_TEST03 demonstrates how the seed can be reset to its initial or last value. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from init_generator import init_generator from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST03' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize() print '' print ' INIT_GENERATOR can reset the seed to the initial value,' print ' the last (previous) value, or a new seed.' # # Set the current generator index to 17. # g = 17 cgn_set(g) print '' print ' Current generator index = %d' % (g) # # Force the current generator to begin at its initial seed. # print '' print ' INIT_GENERATOR ( 0 ) starts at the initial seed.' init_generator(0) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range(1, 10): u = r4_uniform_01() print ' %2d %14.6g' % (i, u) print '' print ' Calling INIT_GENERATOR ( 0 ) again restarts' print ' at the initial seed.' init_generator(0) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range(1, 10): u = r4_uniform_01() print ' %2d %14.6g' % (i, u) print '' print ' Calling INIT_GENERATOR ( 2 ) restarts' print ' at a new "far ahead" seed.' init_generator(2) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range(1, 10): u = r4_uniform_01() print ' %2d %14.6g' % (i, u) print '' print ' Calling INIT_GENERATOR ( 1 ) restarts' print ' at the last seed (in this case, the "far ahead"' print ' seed specified on the previous call.)' print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range(1, 11): u = r4_uniform_01() print ' %2d %14.6g' % (i, u) if ((i % 3) == 0): init_generator(1) print ' (Reset to last seed)'
def initialize ( ): #*****************************************************************************80 # ## INITIALIZE initializes the random number generator library. # # 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: # # None # from antithetic_set import antithetic_set from cgn_set import cgn_set from initialized_set import initialized_set from set_initial_seed import set_initial_seed g_max = 32 # # Remember that we have called INITIALIZE(). # initialized_set ( ) # # Initialize all generators to have FALSE antithetic value. # value = False for g in range ( 1, g_max + 1 ): cgn_set ( g ) antithetic_set ( value ) # # Set the initial seeds. # ig1 = 1234567890 ig2 = 123456789 set_initial_seed ( ig1, ig2 ) # # Initialize the current generator index to 1. # g = 1 cgn_set ( g ) print '' print 'INITIALIZE - Note:' print ' The RNGLIB package has been initialized.' return
def rnglib_test03 ( ): #*****************************************************************************80 # ## RNGLIB_TEST03 demonstrates how the seed can be reset to its initial or last value. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 May 2013 # # Author: # # John Burkardt # from cgn_set import cgn_set from init_generator import init_generator from initialize import initialize from r4_uniform_01 import r4_uniform_01 print '' print 'RNGLIB_TEST03' print ' R4_UNIFORM_01 ( ) returns a random real number' print ' in [0,1] using the current generator.' # # Initialize the package. # print '' print ' INITIALIZE initializes the random number generator.' print ' It only needs to be called once before using the package.' initialize ( ) print '' print ' INIT_GENERATOR can reset the seed to the initial value,' print ' the last (previous) value, or a new seed.' # # Set the current generator index to 17. # g = 17 cgn_set ( g ) print '' print ' Current generator index = %d' % ( g ) # # Force the current generator to begin at its initial seed. # print '' print ' INIT_GENERATOR ( 0 ) starts at the initial seed.' init_generator ( 0 ) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range ( 1, 10 ): u = r4_uniform_01 ( ) print ' %2d %14.6g' % ( i, u ) print '' print ' Calling INIT_GENERATOR ( 0 ) again restarts' print ' at the initial seed.' init_generator ( 0 ) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range ( 1, 10 ): u = r4_uniform_01 ( ) print ' %2d %14.6g' % ( i, u ) print '' print ' Calling INIT_GENERATOR ( 2 ) restarts' print ' at a new "far ahead" seed.' init_generator ( 2 ) print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range ( 1, 10 ): u = r4_uniform_01 ( ) print ' %2d %14.6g' % ( i, u ) print '' print ' Calling INIT_GENERATOR ( 1 ) restarts' print ' at the last seed (in this case, the "far ahead"' print ' seed specified on the previous call.)' print '' print ' I R4_UNIFORM_01 ( )' print '' for i in range ( 1, 11 ): u = r4_uniform_01 ( ) print ' %2d %14.6g' % ( i, u ) if ( ( i % 3 ) == 0 ): init_generator ( 1 ) print ' (Reset to last seed)'