RNSPH
Generates pseudorandom points on a unit circle or K‑dimensional sphere.
Required Arguments
ZNR by K matrix containing the random Cartesian coordinates on the unit circle or sphere. (Output)
Optional Arguments
NR — Number of random numbers to generate. (Input)
Default: NR = size (Z,1).
K — Dimension of the circle (K = 2) or of the sphere. (Input)
Default: K = size (Z,2).
LDZ — Leading dimension of Z exactly as specified in the dimension statement of the calling program. (Input)
Default: LDZ = size (Z,1).
FORTRAN 90 Interface
Generic: CALL RNSPH (Z [])
Specific: The specific interface names are S_RNSPH and D_RNSPH.
FORTRAN 77 Interface
Single: CALL RNSPH (NR, K, Z, LDZ)
Double: The double precision name is DRNSPH.
Description
Routine RNSPH generates pseudorandom coordinates of points that lie on a unit circle or a unit sphere in K‑dimensional space. For points on a circle (K = 2), pairs of uniform (– 1, 1) points are generated and accepted only if they fall within the unit circle (the sum of their squares is less than 1), in which case they are scaled so as to lie on the circle.
For spheres in three or four dimensions, the algorithms of Marsaglia (1972) are used. For three dimensions, two independent uniform (– 1, 1) deviates U1 and U2 are generated and accepted only if the sum of their squares S1 is less than 1. Then, the coordinates
are formed. For four dimensions, U1, U2, and S1 are produced as described above. Similarly, U3, U4, and S2 are formed. The coordinates are then
and
For spheres in higher dimensions, K independent normal deviates are generated and scaled so as to lie on the unit sphere in the manner suggested by Muller (1959).
Comments
The routine RNSET can be used to initialize the seed of the random number generator. The routine RNOPT can be used to select the form of the generator.
Example
In this example, RNSPH is used to generate two uniform random deviates from the surface of the unit sphere in three space.
 
USE UMACH_INT
USE RNSET_INT
USE RNSPH_INT
 
IMPLICIT NONE
INTEGER K, LDZ, NR
PARAMETER (K=3, LDZ=2)
!
INTEGER I, ISEED, J, NOUT
REAL Z(LDZ,K)
!
CALL UMACH (2, NOUT)
NR = 2
ISEED = 123457
CALL RNSET (ISEED)
CALL RNSPH (Z)
WRITE (NOUT,99999) ((Z(I,J),J=1,K),I=1,NR)
99999 FORMAT (' Coordinates of first point: ', 3F8.4, /, &
' Coordinates of second point:', 3F8.4)
END
Output
 
Coordinates of first point: 0.8893 0.2316 0.3944
Coordinates of second point: 0.1901 0.0396 -0.9810