Previous Topic: NUMERICNext Topic: REPLACE


RANDOM-NUMBER

Purpose

Returns a pseudo-random number based on a seed numeric value. The returned random number is greater than zero and less than 1, and has a length of 9 decimal places.

Syntax

►►─┬─ RANDOM-NUMBER ─┬─ ( random-number-seed ) ───────────────────────────────►◄
   └─ RANdom ────────┘

Parameters

random-number-seed

Specifies the numeric variable data field containing the seed value from which the pseudo-random number is calculated.

Random-number-seed cannot be zero.

Usage

To obtain random numbers:

  1. Set the initial random number seed value at execution time to some varying value, such as TIME. The random seed value must not be zero.

    If the result is set to a fixed value, each execution of the dialog will result in the generation of the same series of pseudo-random numbers.

  2. Move the pseudo-random number returned by the random number function to the seed variable data field. The number returned becomes the next seed value. In this way, the random number function can generate a nonrepeating sequence of 536,870,912 numbers.
  3. Define the seed value with a picture of 9(9) and move the result of the function to a variable with a picture of V9(9).

    The result can be moved back to the seed variable by using the result as a redefinition of the seed value, as follows:

    03 SEED-VALUE PICTURE 9(9).
    03 RESULT-VALUE REDEFINES SEED-VALUE PICTURE V9(9).
    

Example

In the following example, the random number function is used to generate a sequence of ten pseudo-random numbers:

Field descriptions:
    03 SEED-VALUE PICTURE 9(9).
    03 RESULT-VALUE REDEFINES SEED-VALUE PICTURE V9(9).
    03 RANDOM-TABLE PICTURE V9(9) OCCURS 10 TIMES.
Statements:
    MOVE TIME TO SEED-VALUE.
    MOVE 1 TO WK-COUNT.
    WHILE WK-COUNT LE 10
      REPEAT.
        MOVE RANDOM(SEED-VALUE) TO RESULT-VALUE.
        MOVE RESULT-VALUE TO RANDOM-TABLE(WK-COUNT).
        ADD 1 TO WK-COUNT.
      END.