#! /usr/bin/env python3 # def random_permutation ( n, x, seed ): #****************************************************************************80 # # Purpose: # # RANDOM_PERMUTATION applies a random permutation to an array. # # Modified: # # 19 February 2016 # # Author: # # Original C version by Warren Smith. # Python version by John Burkardt. # # Parameters: # # Input, integer N, indicates the size of X. # # Input/output, double X[N+2]. On output, entries X[1] through # X[N] have been randomly permuted. # # Input/output, integer SEED, a seed for the random number generator. # from i4_uniform_ab import i4_uniform_ab for i in range ( 1, n + 1 ): j, seed = i4_uniform_ab ( i, n, seed ) t = x[i] x[i] = x[j] x[j] = t return x def random_permutation_test ( ): #*****************************************************************************80 # ## RANDOM_PERMUTATION_TEST tests RANDOM_PERMUTATION. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 19 February 2016 # # Author: # # John Burkardt # import platform from r8vec_indicator0 import r8vec_indicator0 from r8vec_print import r8vec_print print ( '' ) print ( 'RANDOM_PERMUTATION_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' RANDOM_PERMUTATION randomly permutes entries 1 through N of' ) print ( ' a vector X[0:N+1].' ) n = 5 x = r8vec_indicator0 ( n + 2 ) seed = 123456789 r8vec_print ( n + 2, x, ' Initial X:' ) x = random_permutation ( n, x, seed ) r8vec_print ( n + 2, x, ' Permuted X:' ) # # Terminate. # print ( '' ) print ( 'RANDOM_PERMUTATION_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) random_permutation_test ( ) timestamp ( )