Chapter 12: Random Number Generation > random_sphere

random_sphere

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

Synopsis

#include <imsls.h>

float *imsls_f_random_sphere (int n_random, int k,..., 0)

The type double function is imsls_d_random_sphere.

Required Arguments

int n_random   (Input)
Number of random numbers to generate.

int k (Input)
Dimension of the circle (k = 2) or of the sphere.

Return Value

n_random by k matrix containing the random Cartesian coordinates on the unit circle or sphere. To release this space, use free.

Synopsis with Optional Arguments

#include <imsls.h>

float *imsls_f_random_sphere (int n_random, int k,
IMSLS_RETURN_USER, float r[],
 0)

Optional Arguments

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.

Description

Function 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 slie on the unit sphere in the manner suggested by Muller (1959).

Example

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>

 

int 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);

 }

 

Output

 

                   Coordinates

First point       0.8893      0.2316      0.3944

Second point      0.1901      0.0396     -0.9810

 


RW_logo.jpg
Contact Support