Skip to content

$Random()

$Random()

Returns a random integer from a range specified by its argument.

The format for the $RANDOM function is:

$R[ANDOM](intexpr)
  • The integer expression specifies the upper exclusive limit of a range of integers from which $RANDOM() may pick a result; $RANDOM() never returns a number less than zero (0).
  • If $RANDOM() has an argument less than one (1), it generates a run-time error.
  • $RANDOM can generate numbers up to 2147483646 (that is 2GB - 2).

$RANDOM() provides a tool for generating pseudo-random patterns useful in testing or statistical calculations. $RANDOM() results fall between zero (0) and one less than the argument.

Random number generators use factors from the environment to create sequences of numbers. True random number generation requires a source of what is known as “noise”. Pseudo-random numbers appear to have no pattern, but are developed using interactions between factors that vary in ways not guaranteed to be entirely random. In accordance with the M standard, the GT.M implementation of $RANDOM() produces pseudo-random numbers.

Examples of $RANDOM()

Example:

GTM>for i=1:1:10 write $random(1)
0000000000
GTM>

This shows that when $RANDOM() has an argument of one (1), the result is too confined to be random.

Example:

set x=$random(100)+1*.01

This $RANDOM() example produces a number between 0 and 99. The example then shifts with addition, and scales with multiplication to create a value between .01 and 1.