Product SiteDocumentation Site

7.4.49. RANDOM


>>-RANDOM(--+------------------------------------+--)----------><
            +--max-------------------------------+
            +-+--------+-,-+-------+--+--------+-+
              +--min---+   +--max--+  +--,seed-+

Returns a quasi-random whole number in the range min to max inclusive. If you specify max or min,max, then max minus min cannot exceed 999999999. min and max default to 0 and 999, respectively. To start a repeatable sequence of results, use a specific seed as the third argument. This seed must be a positive whole number from 0 to 999999999.
Here are some examples:

Example 7.62. Builtin function RANDOM

RANDOM()          ->    305
RANDOM(5,8)       ->      7
RANDOM(2)         ->      0  /*  0  to  2    */
RANDOM(, ,1983)   ->    123  /* reproducible */
RANDOM(-5, 5)     ->    -3

Notes:
  1. To obtain a predictable sequence of quasi-random numbers, use RANDOM a number of times, but specify a seed only the first time. For example, to simulate 40 throws of a 6-sided, unbiased die:

    Example 7.63. Builtin function RANDOM with seed

    sequence = RANDOM(1,6,12345)  /* any number would */
                                  /* do for a seed    */
    do 39
    sequence = sequence RANDOM(1,6)
    end
    say sequence
    

    The numbers are generated mathematically, using the initial seed, so that as far as possible they appear to be random. Running the program again produces the same sequence; using a different initial seed almost certainly produces a different sequence. If you do not supply a seed, the first time RANDOM is called, an arbitrary seed is used. Hence, your program usually gives different results each time it is run.
  2. The random number generator is global for an entire program; the current seed is not saved across internal routine calls.