Exemplo n.º 1
0
def antithetic_memory(i, value):

    #*****************************************************************************80
    #
    ## ANTITHETIC_MEMORY stores the antithetic value for all generators.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    27 May 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer I, the desired action.
    #    -1, get a value.
    #    0, initialize all values.
    #    1, set a value.
    #
    #    Input/output, bool VALUE.  For I = -1, VALUE is an output
    #    quantity, for I = +1, VALUE is an input quantity.
    #
    from cgn_get import cgn_get

    g_max = 32

    if (i < 0):
        g = cgn_get()
        value = antithetic_memory.a_save[g - 1]
    elif (i == 0):
        antithetic_memory.a_save = []
        for i in range(1, g_max + 1):
            antithetic_memory.a_save.append(False)
        value = false
    elif (0 < i):
        g = cgn_get()
        antithetic_memory.a_save[g - 1] = value

    return value
def antithetic_memory ( i, value ):

#*****************************************************************************80
#
## ANTITHETIC_MEMORY stores the antithetic value for all generators.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    27 May 2013
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer I, the desired action.
#    -1, get a value.
#    0, initialize all values.
#    1, set a value.
#
#    Input/output, bool VALUE.  For I = -1, VALUE is an output
#    quantity, for I = +1, VALUE is an input quantity.
#
  from cgn_get import cgn_get

  g_max = 32

  if ( i < 0 ):
    g = cgn_get ( )
    value = antithetic_memory.a_save[g-1]
  elif ( i == 0 ):
    antithetic_memory.a_save = [];
    for i in range ( 1, g_max + 1 ):
      antithetic_memory.a_save.append ( False )
    value = false
  elif ( 0 < i ):
    g = cgn_get ( )
    antithetic_memory.a_save[g-1] = value

  return value
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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