IBM Books

Engineering and Scientific Subroutine Library for AIX Version 3 Release 3: Guide and Reference

SNRAND and DNRAND--Generate a Vector of Normally Distributed Random Numbers

These subroutines generate vector x of normally distributed pseudo-random numbers, with a mean of 0 and a standard deviation of 1, using Polar methods with a user-specified seed.

Table 169. Data Types

x, aux seed Subroutine
Short-precision real Long-precision real SNRAND
Long-precision real Long-precision real DNRAND

Syntax

Fortran CALL SNRAND | DNRAND (seed, n, x, aux, naux)
C and C++ snrand | dnrand (seed, n, x, aux, naux);
PL/I CALL SNRAND | DNRAND (seed, n, x, aux, naux);

On Entry

seed
is the initial value used to generate the random numbers. Specified as: a number of the data type indicated in Table 169. It must be a whole number; that is, the fraction part must be 0. Its value must be 1.0 <= seed < (2147483647.0 = 231-1).

Note:
seed is always a long-precision real number, even in SNRAND.

n
is the number of random numbers to be generated. Specified as: a fullword integer; n must be an even number and n >= 0.

x
See On Return.

aux
has the following meaning:

If naux = 0 and error 2015 is unrecoverable, aux is ignored.

Otherwise, it is the storage work area used by this subroutine. Its size must be greater than or equal to n/2.

Specified as: an area of storage, containing numbers of the data type indicated in Table 169. They can have any value.

naux
is the size of the work area specified by aux. Specified as: a fullword integer, where:

If naux = 0 and error 2015 is unrecoverable, SNRAND and DNRAND dynamically allocate the work area used by the subroutine. The work area is deallocated before control is returned to the calling program.

Otherwise, naux >= n/2.

On Return

seed
is the new seed that is to be used to generate additional random numbers in subsequent invocations of SNRAND or DNRAND. Returned as: a number of the data type indicated in Table 169. It is a whole number whose value is 1.0 <= seed < (2147483647.0 = 231-1).

x
is a vector of length n, containing the normally distributed pseudo-random numbers. Returned as: a one-dimensional array of (at least) length n, containing numbers of the data type indicated in Table 169.

Notes
  1. In your C program, argument seed must be passed by reference.
  2. Vector x must have no common elements with the storage area specified for aux; otherwise, results are unpredictable.
  3. You have the option of having the minimum required value for naux dynamically returned to your program. For details, see Using Auxiliary Storage in ESSL.

Function

The normally distributed pseudo-random numbers, with a mean of 0 and a standard deviation of 1, are generated as follows, using Polar methods with a user-specified seed. The Polar method, which this technique is based on, was developed by G. E. P. Box, M. E. Muller, and G. Marsaglia and is described in reference [76].

  1. Using seed, a vector of uniform (0,1) pseudo-random numbers, ui for i = 1, n, is generated by calling SURAND or DURAND, respectively. These ui values are then used in the subsequent steps.
  2. All (yj, zj) for j = 1, n/2 are set as follows, where each (y, z) is a point in the square -1 to 1:
    yj = 2u2j-1-1
    zj = 2u2j-1
  3. All pj for j = 1, n/2 are set as follows, where each p measures the square of the radius of (y, z):



    Random Number Generator Graphic

    If pj >= 1, then pj is discarded, and steps 1 through 3 are repeated until pj < 1.

  4. All xi for i = 1, n are set as follows to produce the normally distributed random numbers:
    x2j-1 = yj ((-2 ln pj) / pj)0.5
    x2j = zj ((-2 ln pj) / pj)0.5
       for j = 1, n/2

If n is 0, no computation is performed, and the initial seed is unchanged.

Error Conditions

Resource Errors

Error 2015 is unrecoverable, naux = 0, and unable to allocate work area.

Computational Errors

None

Input-Argument Errors
  1. n < 0 or n is an odd number
  2. seed < 1.0 or seed >= 2147483647.0
  3. Error 2015 is recoverable or naux<>0, and naux is too small--that is, less than the minimum required value. Return code 1 is returned if error 2015 is recoverable.

Example 1

This example shows a call to SNRAND to generate 10 random numbers.

Call Statement and Input
             SEED   N    X   AUX  NAUX
               |    |    |    |    |
CALL SNRAND( SEED , 10 , X , AUX , 5  )
 
SEED     =  80629.0
Note:
It is important to note that SEED is a long-precision number, even though X contains short-precision numbers.

Output
SEED     =  48669425.0
 
X        =  (0.660649538,
             1.312503695,
             1.906438112,
             0.014065863,
            -0.800935328,
            -3.058144093,
            -0.397426069,
            -0.370634943,
            -0.064151444,
            -0.275887042)

Example 2

This example shows a call to DNRAND to generate 10 random numbers.

Call Statement and Input
             SEED   N    X   AUX  NAUX
               |    |    |    |    |
CALL DNRAND( SEED , 10 , X , AUX , 5  )
 
SEED     =  80629.0

Output
SEED     =  48669425.0
 
X        =  (0.6606495655963802,
             1.3125037758861060,
             1.9064381379483730,
             0.0140658628770495,
            -0.8009353314494653,
            -3.0581441239248530,
            -0.3974260845722100,
            -0.3706349643478605,
            -0.0641514443372939,
            -0.2758870630332470)


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]