util.Math.rand

Returns a positive pseudo-random number.

Syntax

util.Math.rand(
   max INTEGER )
  RETURNS INTEGER
  1. max is the maximum (exclusive) random number that can be generated: The greatest integer number the function may produce is (max - 1).

Usage

Important:

The srand() function initializes the pseudo-random numbers generator. It must be called before subsequent calls to the rand() function. If you do not call the srand() function, the rand() function will generate the same sequence of numbers for every program execution. The numbers generated by rand() can vary depending on the operating system.

The rand() function returns a pseudo-random integer number between zero (inclusive) and max - 1 (the value max is exclusive). For example, rand(4) can return 0, 1, 2 or 3.

The greatest random number that rand() can produce is 2,147,483,646, which is the biggest positive value that can be represented by an INTEGER type, minus one.

The rand() function returns zero, if the argument is lower or equal to 0.

Example

IMPORT util
MAIN
    DEFINE i SMALLINT
    DISPLAY "Before srand() call:"
    FOR i=1 TO 3
        DISPLAY util.Math.rand(100)
    END FOR
    CALL util.Math.srand()
    DISPLAY "After srand() call:"
    FOR i=1 TO 3
        DISPLAY util.Math.rand(100)
    END FOR
END MAIN

(run this example several times)