Generates pseudorandom points on a unit circle or K-dimensional sphere
#include <imsls.h>
float *imsls_f_random_sphere (int n_random, int k,..., 0)
The type double function is imsls_d_random_sphere.
int n_random
(Input)
Number of random numbers to generate.
int k (Input)
Dimension of the circle (k = 2) or of the sphere.
n_random by k matrix containing the random Cartesian coordinates on the unit circle or sphere. To release this space, use free.
#include <imsls.h>
float
*imsls_f_random_sphere (int
n_random,
int k,
IMSLS_RETURN_USER, float
r[],
0)
IMSLS_RETURN_USER, float r[]
(Output)
User-supplied array of size n_random by k containing the random Cartesian coordinates on the unit
circle or sphere.
Routine imsls_f_random_sphere 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).
In this example, imsls_f_random_sphere is used to generate two uniform random deviates from the surface of the unit sphere in three space.
#include <stdio.h>
#include <imsls.h>
void main()
{
int n_random = 2;
int k = 3;
float *z;
char *rlabel[] = {"First point",
"Second point"};
imsls_random_seed_set(123457);
z = imsls_f_random_sphere(n_random, k, 0);
imsls_f_write_matrix("Coordinates", n_random, k, z,
IMSLS_ROW_LABELS, rlabel,
IMSLS_NO_COL_LABELS,
0);
}
Coordinates
First point 0.8893 0.2316 0.3944
Second point 0.1901 0.0396 -0.9810
Visual Numerics, Inc. PHONE: 713.784.3131 FAX:713.781.9260 |