#! /usr/bin/env python # def ball01_sample ( n, seed ): #*****************************************************************************80 # ## BALL01_SAMPLE uniformly samples the unit ball. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 June 2015 # # Author: # # John Burkardt # # Reference: # # Russell Cheng, # Random Variate Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, pages 168. # # Reuven Rubinstein, # Monte Carlo Optimization, Simulation, and Sensitivity # of Queueing Networks, # Krieger, 1992, # ISBN: 0894647644, # LC: QA298.R79. # # Parameters: # # Input, integer N, the number of points. # # Input/output, integer SEED, a seed for the random # number generator. # # Output, real X(3,N), the points. # import numpy as np from r8_uniform_01 import r8_uniform_01 from r8vec_normal_01 import r8vec_normal_01 x = np.zeros ( [ 3, n ] ) for j in range ( 0, n ): # # Fill a vector with normally distributed values. # v, seed = r8vec_normal_01 ( 3, seed ) # # Compute the length of the vector. # norm = np.sqrt ( v[0] ** 2 + v[1] ** 2 + v[2] ** 2 ) # # Normalize the vector. # for i in range ( 0, 3 ): v[i] = v[i] / norm # # Transfer the point from the surface to the interior. # r, seed = r8_uniform_01 ( seed ) r = r ** ( 1.0 / 3.0 ) for i in range ( 0, 3 ): x[i,j] = r * v[i] return x, seed def ball01_sample_test ( ): #*****************************************************************************80 # ## BALL01_SAMPLE_TEST tests BALL01_SAMPLE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 June 2015 # # Author: # # John Burkardt # import platform from r8mat_transpose_print import r8mat_transpose_print print ( '' ) print ( 'BALL01_SAMPLE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' BALL01_SAMPLE samples the unit ball.' ) n = 10 seed = 123456789 x, seed = ball01_sample ( n, seed ) r8mat_transpose_print ( 3, n, x, ' Sample points in the unit ball.' ) # # Terminate. # print ( '' ) print ( 'BALL01_SAMPLE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ball01_sample_test ( ) timestamp ( )