#! /usr/bin/env python # def ubvec_random ( n, seed ): #*****************************************************************************80 # ## UBVEC_RANDOM returns a pseudorandom BVEC. # # Discussion: # # A UBVEC is a vector of N binary digits. # # A UBVEC can be interpreted as a binary representation of an # unsigned integer, with the first entry being the coefficient of # 2^(N-1) and the last entry the coefficient of 1. # # UBVEC # # ----- -- # 00000 0 # 00001 1 # 00010 2 # 10000 16 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # 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 N, the order of the vector. # # Input/output, integer SEED, the "seed" value, which should # NOT be 0. On output, SEED has been updated. # # Output, integer UBVEC(N), a pseudorandom binary vector. # import numpy as np from sys import exit i4_huge = 2147483647 i4_huge_half = 1073741823 if ( seed == 0 ): print ( '' ) print ( 'UBVEC_RANDOM - Fatal error!' ) print ( ' Input value of SEED = 0.' ) exit ( 'UBVEC_RANDOM - Fatal error!' ) ubvec = np.zeros ( n ) for i in range ( 0, n ): k = ( seed // 127773 ) seed = 16807 * ( seed - k * 127773 ) - k * 2836 if ( seed < 0 ): seed = seed + i4_huge if ( i4_huge_half < seed ): ubvec[i] = 0 else: ubvec[i] = 1 return ubvec, seed def ubvec_random_test ( ): #*****************************************************************************80 # ## UBVEC_RANDOM_TEST tests UBVEC_RANDOM. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # import platform from ubvec_print import ubvec_print n = 5 print ( '' ) print ( 'UBVEC_RANDOM_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' UBVEC_RANDOM randomizes an unsigned binary vector.' ) print ( '' ) seed = 123456789 for i in range ( 0, 5 ): ubvec, seed = ubvec_random ( n, seed ) ubvec_print ( n, ubvec, '' ) # # Terminate. # print ( '' ) print ( 'UBVEC_RANDOM_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ubvec_random_test ( ) timestamp ( )