Skip to main content

getRandomNumber

Returns a randomly selected decimal number from between the given lower and upper bounds.

The lower bound is inclusive and the upper bound is exclusive.

Tip

To get a random number from a specific set of options, use getRandomInteger or chooseAtRandom instead. This is explained further below in When to use other functions.

Parameters

  • LOWER BOUND (number)

    Optional. Defines the lower bound for the return value. Must be a number (decimal or integer) and lower than the UPPER BAND value. The lower bound is inclusive.

    Default: 0.0

  • UPPER BOUND (number)

    Optional. Defines the upper bound for the return value. Must be a number (decimal or integer) and higher than the LOWER BAND value. The upper bound is exclusive.

    Default: 1.0

Examples

If the parameters are unspecified, the default values apply.

ATL in Script

Result

[[getRandomInteger()]]

A number between 0.0 (inclusive) and 1.0 (exclusive).

[[getRandomNumber(0.5,'')]]

A number between 0.5 (inclusive) and 1.0 (exclusive). T

Use the first and second parameters to set upper and lower bounds.

ATL in Script

Result

[[getRandomNumber(1,5)]]

A number between 1.0 (inclusive) and 5.0 (exclusive).

[[getRandomNumber(-1.22,5.456)]]

A number between -1.22 (inclusive) and 5.456 (exclusive).

Display value vs. actual value

The function returns a random decimal, so the result typically has many decimal places (though the number can vary). The result for [[getRandomNumber(1,5)]] might be 1.662116239238458970106648848741.

The result's display value — the number of decimal places shown when you preview — is determined by your project's NUMBER FORMAT setting. You can override this setting with a formatting function.

ATL in Script

Notes

[[numberFormat(getRandomNumber(1,5),'#.####')]]

The result is displayed to four decimal places. The result's actual value is unchanged.

However, the result's full actual value is used when the result is fed into another arithmetic operation. To change the result's actual value, use the round function instead:

ATL in Script

Notes

[[round(getRandomNumber(1,5),3)]]

The result is rounded and displayed to three decimal places. The result's actual value is changed.

When to use other functions

The getRandomNumber function is unsuitable if you want a randomly selected number from a specific set of options.

Let's say you want to randomly select one number from three options: 0.1, 0.2, and 0.3.

The ATL [[getRandomNumber(0.1,0.4)]] is unsuitable because 0.1, 0.2, and 0.3 are not the only numbers that exist between the bounds. Instead, use getRandomInteger and scale the result:

ATL in Script

Result

[[getRandomInteger(1,4)/10]]

0.1, 0.2, or 0.3.

Or use the chooseAtRandom function:

ATL in Script

Result

[[chooseAtRandom(0.1,0.2,0.3)]]

0.1, 0.2, or 0.3.