randomSphere

Generates pseudorandom points on a unit circle or K-dimensional sphere

Synopsis

randomSphere (nRandom, k)

Required Arguments

int nRandom (Input)
Number of random numbers to generate.
int k (Input)
Dimension of the circle (k = 2) or of the sphere.

Return Value

nRandom by k matrix containing the random Cartesian coordinates on the unit circle or sphere.

Description

Function randomSphere 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 \(U_1\) and \(U_2\) are generated and accepted only if the sum of their squares \(S_1\) is less than 1. Then, the coordinates

\[Z_1 = 2U_1 \sqrt{1-S_1}, Z_2 = 2U_2 \sqrt{1-S_1} \text{, and } Z_3 = 1 - 2S_1\]

are formed. For four dimensions, \(U_1\), \(U_2\), and \(S_1\) are produced as described above. Similarly, \(U_3\), \(U_4\), and \(S_2\) are formed. The coordinates are then

\[Z_1 = U_1, Z_2 = U_2, Z_3 = U_3 \sqrt{1 - S_1/S_2}\]

and

\[Z_4 = U_4\sqrt{1 - S_1/S_2}\]

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).

Example

In this example, randomSphere is used to generate two uniform random deviates from the surface of the unit sphere in three space.

from numpy import *
from pyimsl.stat.randomSphere import randomSphere
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

n_random = 2
k = 3
rlabel = ["First point", "Second point"]
randomSeedSet(123457)
r = randomSphere(n_random, k)
writeMatrix("Coordinates", r,
            rowLabels=rlabel,
            noColLabels=True)

Output

 
                    Coordinates
First point        0.8893       0.2316       0.3944
Second point       0.1901       0.0396      -0.9810