Returns a pseudo-random integer value in the specified range.
Examples
The following example returns a random number from 0 through 24 (inclusive).
WRITE $RANDOM(25)
To return a random number with a fractional portion, you can use the concatenation operator (_) or the addition operator (+), as shown in the following example:
SET x=$RANDOM(10)_$RANDOM(10)/10
WRITE !,x
SET y=$RANDOM(10)+($RANDOM(10)/10)
WRITE !,y
This program returns numbers with one fractional digit, ranging between .0 and 9.9 (inclusive). Using either operator, InterSystems IRIS deletes any leading and trailing zeros (and the decimal point, if the fractional portion is zero). However, if both $RANDOM functions return zero (0 and .0), InterSystems IRIS returns a zero (0).
The following example simulates the roll of two dice:
Dice
FOR {
READ "Roll dice? ",reply#1
IF "Yy"[reply,reply'="" {
WRITE !,"Pair of dice: "
WRITE $RANDOM(6)+1,"+",$RANDOM(6)+1,! }
ELSE { QUIT }
}