#! /usr/bin/env python # def r8col_uniform_ab ( m, n, a, b, seed ): #*****************************************************************************80 # ## R8COL_UNIFORM_AB returns a scaled pseudorandom R8COL. # # Discussion: # # An R8COL is an M by N array of R8's, regarded as an array of N columns, # each of length M. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 February 2016 # # Author: # # John Burkardt # # Reference: # # Paul Bratley, Bennett Fox, Linus Schrage, # A Guide to Simulation, # Second Edition, # Springer, 1987, # ISBN: 0387964673, # LC: QA76.9.C65.B73. # # Bennett Fox, # Algorithm 647: # Implementation and Relative Efficiency of Quasirandom # Sequence Generators, # ACM Transactions on Mathematical Software, # Volume 12, Number 4, December 1986, pages 362-376. # # Pierre L'Ecuyer, # Random Number Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, # ISBN: 0471134031, # LC: T57.62.H37. # # Peter Lewis, Allen Goodman, James Miller, # A Pseudo-Random Number Generator for the System/360, # IBM Systems Journal, # Volume 8, Number 2, 1969, pages 136-143. # # Parameters: # # Input, integer M, N, the number of rows and columns in the array. # # Input, real A, B, the range of the pseudorandom values. # # Input, integer SEED, the integer "seed" used to generate # the output random number. # # Output, real R(M,N), an array of random values between A and B. # # Output, integer SEED, the updated seed. This would # normally be used as the input seed on the next call. # import numpy as np from sys import exit i4_huge = 2147483647 seed = np.floor ( seed ) if ( seed < 0 ): seed = seed + i4_huge if ( seed == 0 ): print '' print 'R8COL_UNIFORM_AB - Fatal error!' print ' Input SEED = 0!' exit ( 'R8COL_UNIFORM_AB - Fatal error!' ) r = np.zeros ( [ m, n ] ) for j in range ( 0, n ): for i in range ( 0, m ): k = np.floor ( seed / 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 seed = np.floor ( seed ) seed = ( seed % i4_huge ) if ( seed < 0 ): seed = seed + i4_huge r[i,j] = a + ( b - a ) * seed * 4.656612875E-10 return r, seed def r8col_uniform_ab_test ( ): #*****************************************************************************80 # ## R8COL_UNIFORM_AB_TEST tests R8COL_UNIFORM_AB. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 February 2016 # # Author: # # John Burkardt # import numpy as np from r8col_print import r8col_print m = 5 n = 4 a = -1.0 b = +5.0 seed = 123456789 print '' print 'R8COL_UNIFORM_AB_TEST' print ' R8COL_UNIFORM_AB computes a random R8COL.' print '' print ' %g <= X <= %g' % ( a, b ) print ' Initial seed is %d' % ( seed ) v, seed = r8col_uniform_ab ( m, n, a, b, seed ) r8col_print ( m, n, v, ' Random R8COL:' ) # # Terminate. # print '' print 'R8COL_UNIFORM_AB_TEST:' print ' Normal end of execution.' return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8col_uniform_ab_test ( ) timestamp ( )