IBM Books

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

SURAND and DURAND--Generate a Vector of Uniformly Distributed Random Numbers

These subroutines generate vector x of uniform (0,1) pseudo-random numbers, using the multiplicative congruential method with a user-specified seed.

Table 168. Data Types

x seed Subroutine
Short-precision real Long-precision real SURAND
Long-precision real Long-precision real DURAND
Note:
If you need a very long period random number generator, use SURXOR and DURXOR instead of these subroutines.

Syntax

Fortran CALL SURAND | DURAND (seed, n, x)
C and C++ surand | durand (seed, n, x);
PL/I CALL SURAND | DURAND (seed, n, x);

On Entry

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

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

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

x
See On Return.

On Return

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

x
is a vector of length n, containing the uniform pseudo-random numbers with values between 0 and 1. Returned as: a one-dimensional array of (at least) length n, containing numbers of the data type indicated in Table 168.

Note

In your C program, argument seed must be passed by reference.

Function

The uniform (0,1) pseudo-random numbers are generated as follows, using the multiplicative congruential method:

si = (a(si-1)) mod(m) = (ais0) mod(m)
xi = si/m    for i = 1, 2, ..., n

where:

si is a random sequence.
xi is a random number.
s0 is the initial seed provided by the caller.
a = 75 = 16807.0
m = 231-1 = 2147483647.0
n is the number of random numbers to be generated.

See references [76] and [80]. If n is 0, no computation is performed, and the initial seed is unchanged.

Error Conditions

Computational Errors

None

Input-Argument Errors
  1. n < 0
  2. seed < 1.0 or seed >= 2147483647.0

Example 1

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

Call Statement and Input
             SEED    N   X
               |     |   |
CALL SURAND( SEED , 10 , X )
 
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     =  759150100.0
 
X        =  (0.6310323,
             0.7603202,
             0.7015232,
             0.5014868,
             0.4895853,
             0.4602344,
             0.1603608,
             0.1832564,
             0.9899062,
             0.3535068)

Example 2

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

Call Statement and Input
             SEED    N   X
               |     |   |
CALL DURAND( SEED , 10 , X )
 
SEED     =  80629.0

Output
SEED     =  759150100.0
 
X        =  (0.6310323270182275,
             0.7603201953509451,
             0.7015232633340746,
             0.5014868557925740,
             0.4895853057920864,
             0.4602344475967038,
             0.1603607578018497,
             0.1832563756887132,
             0.9899062002030695,
             0.3535068129904134)


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