Previous Topic Next topic Print topic


RANDOM Function

Purpose

Returns a random number generated using an expression as the given seed.

Syntax

RANDOM(x)

Parameters

x is an expression that must have a computational type and should have an arithmetic type.

Description

The RANDOM function returns a random number generated using an expression x as the given seed. If the expression is omitted, the random number is generated using the seed of the last RANDOM function that has a seed. If there is no RANDOM function with a seed invoked before that, the default seed is 1.

If the expression x that is used as a seed is numeric, it must be real. If it is not specified FIXED BINARY(31,0), it is converted.

If x is not greater than 0 and less than 2,147,483,646, the ERROR condition is raised.

The output of RANDOM is a value distributed between 0 and 1. It is generated using the following multiplicative congruential method:

seed(x) = mod(950706376 * seed(x - 1), 2147483647)
random(x) = seed(x) / 2147483647

The seed is maintained at the program level.

Examples

rand: proc options(main);

    dcl x float bin (52);
    dcl i fixed bin (31);

    put skip list ('---- seed: 10 -------------');
    x = random(10);
    put skip list (x);

    do i = 1 to 6;
       x = random();
       put skip list (x);
    end;

    put skip list ('---- seed: 100000 ---------');

    x = random(100000);
    put skip list (x);

    do i = 1 to 6;
       x = random();
       put skip list (x);
    end;
end;

will print:

---- seed: 10 -------------
 9.843643940912393E-001
 3.341328461347766E-001
 1.034361082610842E-001
 5.866471489829230E-001
 7.405739988854965E-001
 3.124010266328235E-001
 7.504689692288959E-001
---- seed: 100000 ---------
 2.727828287858436E-001
 4.692549409667286E-001
 1.388299465825921E-001
 3.343486782788991E-001
 9.890356291965748E-002
 8.330502211270156E-001
 3.848663831059199E-001

Restrictions

None.

Previous Topic Next topic Print topic