The function RANDOM returns a pseudorandom number within a specified range.
This function takes three pieces of input: a minimum Number (which is optional, the default value is 0), a maximum Number (which is optional, the default value is 1), and a boolean (which is optional and can only be used if a minimum and maximum number are entered). It will output a pseudorandom Number between the minimum and maximum numbers. If the boolean value is TRUE, this output will be an integer; if the boolean value is FALSE(the default value), this output will be any Number.
Declaration
RANDOM() -> random_number
RANDOM(max) -> random_number
RANDOM(min, max) -> random_number
RANDOM(min, max, require_integer) -> random_number
Parameters
min (optional, type: number)
The minimum number in the range from which the pseudorandom number is selected. The default value of this variable is 0.
The range the pseudorandom number is selected from is non-inclusive. That is, the output of the RANDOM function will never be exactly the value given for min.
max (optional, type: number)
The maximum number in the range from which the pseudorandom number is selected. The default value of this variable is 1.
The range the pseudorandom number is selected from is non-inclusive. That is, the output of the RANDOM function will never be exactly the value given for max.
require_integer(optional, type: boolean)
A boolean indicating whether the return value should be an integer or any real number. TRUEmeans that is must be an integer, FALSE means that it can be any real number.
This variable is optional and must be used in combination with min and max. The default value is FALSE.
Return Values
random_number (type: number)
A pseudorandom number between in the range specified.
Examples
The following examples provides a pseudorandom number between 0 and 1, the default values for min and max. Note that the number it outputs is not an integer. Also note that there is not one particular outcome associated with this function. Each time it is called upon, it outputs a different number between 0 and 1.
RANDOM() -> 0.473562840948746
RANDOM() -> 0.209858654278889
RANDOM() -> 0.937460900977645
The following examples provides a pseudorandom number between 0 and 5. Again note that the number it outputs is not an integer, and that there is not one particular outcome associated with this function. Each time it is called on, it provides a different pseudorandom number between 0 and 5:
RANDOM(5) -> 3.758997665480921
RANDOM(5) -> 4.132364836251733
RANDOM(5) -> 1.845748992284799
The following examples provides a pseudorandom number between 5 and 10. Again note that the number it outputs is not an integer, and that there is not one particular outcome associated with this function. Each time it is called on, it provides a different pseudorandom number between 5 and 10:
RANDOM(5, 10) -> 6.606093230420562
RANDOM(5, 10) -> 8.017837262846251
RANDOM(5, 10) -> 7.998362648372525
If only pseudorandom integers are desired as output, the optional variable require_integer must be set to TRUE. (Note that this can only be done if a value is explicitly given for min.) The following example provides a pseudorandom integer between 0 and 10. Note that 0 and 10 do not appear; the numbers specifying the range are excluded:
RANDOM(0, 10, TRUE) -> 9
RANDOM(0, 10, TRUE) -> 3
RANDOM(0, 10, TRUE) -> 7
The optional variable require_integer can also be explicitly set to FALSE, and the function will behave exactly as though the default value was being used. The last example outputs any pseudorandom real number between 5 and 10:
RANDOM(5, 10, FALSE) -> 5.189786409853899
RANDOM(5, 10, FALSE) -> 9.583098649737689
RANDOM(5, 10, FALSE) -> 8.604847369012587