Go to the first, previous, next, last section, table of contents.

The Spherical Distribution (2D & 3D)

The spherical distributions generate random vectors, located on a spherical surface. They can be used as random directions, for example in the steps of a random walk.

Random: void gsl_ran_dir_2d (const gsl_rng * r, double *x, double *y)
Random: void gsl_ran_dir_2d_trig_method (const gsl_rng * r, double *x, double *y)
This function returns a random direction vector @math{v} = (x,y) in two dimensions. The vector is normalized such that @math{|v|^2 = x^2 + y^2 = 1}. The obvious way to do this is to take a uniform random number between 0 and @math{2\pi} and let x and y be the sine and cosine respectively. Two trig functions would have been expensive in the old days, but with modern hardware implementations, this is sometimes the fastest way to go. This is the case for my home Pentium (but not the case for my Sun Sparcstation 20 at work). Once can avoid the trig evaluations by choosing x and y in the interior of a unit circle (choose them at random from the interior of the enclosing square, and then reject those that are outside the unit circle), and then dividing by @c{$\sqrt{x^2 + y^2}$} @math{\sqrt{x^2 + y^2}}. A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd ed, p140, exercise 23), requires neither trig nor a square root. In this approach, u and v are chosen at random from the interior of a unit circle, and then @math{x=(u^2-v^2)/(u^2+v^2)} and @math{y=uv/(u^2+v^2)}.

Random: void gsl_ran_dir_3d (const gsl_rng * r, double *x, double *y, double * z)
This function returns a random direction vector @math{v} = (x,y,z) in three dimensions. The vector is normalized such that @math{|v|^2 = x^2 + y^2 + z^2 = 1}. The method employed is due to Robert E. Knop (CACM 13, 326 (1970)), and explained in Knuth, v2, 3rd ed, p136. It uses the surprising fact that the distribution projected along any axis is actually uniform (this is only true for 3d).

Random: void gsl_ran_dir_nd (const gsl_rng * r, int n, double *x)

This function returns a random direction vector @math{v = (x_1,x_2,...,x_n)} in n dimensions. The vector is normalized such that @math{|v|^2 = x_1^2 + x_2^2 + ... + x_n^2 = 1}. The method uses the fact that a multivariate gaussian distribution is spherically symmetric. Each component is generated to have a gaussian distribution, and then the components are normalized. The method is described by Knuth, v2, 3rd ed, p135-136, and attributed to G. W. Brown, Modern Mathematics for the Engineer (1956).


Go to the first, previous, next, last section, table of contents.