コード例 #1
0
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)
コード例 #2
0
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 )
コード例 #3
0
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 ''
コード例 #4
0
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 ''
コード例 #5
0
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)'
コード例 #6
0
ファイル: lcrg_seed.py プロジェクト: wangwt9907/jburkardt-py
def lcrg_seed_test():

    #*****************************************************************************80
    #
    ## LCRG_SEED_TEST tests LCRG_SEED.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    05 April 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    from r4_uniform_01 import r4_uniform_01

    print ''
    print 'LCRG_SEED_TEST'
    print '  LCRG_SEED directly computes the updated value of a'
    print '  seed used by an linear congruential random number'
    print '  generator.'
    print ''
    print '       I          SEED          SEED          SEED    U'
    print '                 Input        Output          LCRG'
    print ''
    #
    #  These parameters define the old (1969) IBM 360 random number generator:
    #
    a = 16807
    b = 0
    c = 2147483647
    #
    #  This seed value was used in Pierre L'Ecuyer's article.
    #
    seed_start = 12345

    seed = seed_start
    #
    #  Compute 1000 random numbers "the hard way", that is, sequentially.
    #  Every now and then, call LCRG_SEED to compute SEED directly.
    #
    for i in range(1, 1001):

        seed_in = seed
        [u, seed] = r4_uniform_01(seed)
        seed_out = seed

        if (i <= 10 or i == 100 or i == 1000):

            seed_lcrg = lcrg_seed(a, b, c, i, seed_start)

            print '  %6d  %12d  %12d  %12d  %14f' \
            % ( i, seed_in, seed_out, seed_lcrg, u )

    print ''
    print 'LCRG_SEED_TEST'
    print '  Normal end of execution.'

    return
コード例 #7
0
def lcrg_seed_test ( ):

#*****************************************************************************80
#
## LCRG_SEED_TEST tests LCRG_SEED.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    05 April 2013
#
#  Author:
#
#    John Burkardt
#
  from r4_uniform_01 import r4_uniform_01

  print ''
  print 'LCRG_SEED_TEST'
  print '  LCRG_SEED directly computes the updated value of a'
  print '  seed used by an linear congruential random number'
  print '  generator.'
  print ''
  print '       I          SEED          SEED          SEED    U'
  print '                 Input        Output          LCRG'
  print ''
#
#  These parameters define the old (1969) IBM 360 random number generator:
#
  a = 16807
  b = 0
  c = 2147483647
#
#  This seed value was used in Pierre L'Ecuyer's article.
#
  seed_start = 12345

  seed = seed_start
#
#  Compute 1000 random numbers "the hard way", that is, sequentially.
#  Every now and then, call LCRG_SEED to compute SEED directly.
#
  for i in range ( 1, 1001 ):

    seed_in = seed
    [ u, seed ] = r4_uniform_01 ( seed )
    seed_out = seed

    if ( i <= 10 or i == 100 or i == 1000 ):

      seed_lcrg = lcrg_seed ( a, b, c, i, seed_start )

      print '  %6d  %12d  %12d  %12d  %14f' \
      % ( i, seed_in, seed_out, seed_lcrg, u );

  print ''
  print 'LCRG_SEED_TEST'
  print '  Normal end of execution.'

  return
コード例 #8
0
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)'