HP 3000 Manuals

RANDOM Function [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

RANDOM Function 

The RANDOM function returns a numeric value that is a pseudo-random
number for a rectangular distribution.  The function type is numeric.

Syntax 

     FUNCTION RANDOM [(parameter-1)]

Parameters 

parameter-1           parameter-1 is used as the seed value to generate a
                      sequence of pseudo-random numbers.  If specified,
                      must be in integer between zero and 999999999,
                      inclusive.

                      If a subsequent reference specifies parameter-1, a
                      new sequence of pseudo-random numbers is started.

                      If the first reference to this function in the run
                      unit does not specify parameter-1, the seed value
                      is zero.  In each case, subsequent references
                      without specifying parameter-1 returns the next
                      number in the current sequence.

                      If parameter-1 is specified, parameter-1 determines
                      a sequence of pseudo-random numbers.  Each time a
                      value is returned, that 32-bit value is used in
                      place of parameter-1 to continue the pseudo-random
                      sequence.

                      For details on the RANDOM function, refer to the
                      Compiler Library/XL Reference Manual.

Return Value 

The returned value is greater than or equal to zero and less than one.
For a given seed value, the sequence of pseudo-random numbers is always
the same.

Example 

     77  RANDOM-NUMBER    PIC V99999 VALUE ZERO.
            :
     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM (1)
     DISPLAY RANDOM-NUMBER
     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM
     DISPLAY RANDOM-NUMBER
     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM
     DISPLAY RANDOM-NUMBER

     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM (2893)
     DISPLAY RANDOM-NUMBER
     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM
     DISPLAY RANDOM-NUMBER
     COMPUTE RANDOM-NUMBER = FUNCTION RANDOM
     DISPLAY RANDOM-NUMBER

The above example displays the following:

     .04163
     .36480
     .73704
     .40190
     .06571
     .61402



MPE/iX 5.0 Documentation