User Tools

Site Tools


syntax:functions:random

Random()

Category: Number function


Description

This function generates a random number between 0 and 1.


Arguments

This function does not require arguments.

Return value type: Number


Remarks

Generating random numbers within a given range

To generate a random number between a specific minimum and maximum value, use the following calculation (extra spaces have been added for clarity):

( random() * ( [number of values in range] - 1) ) + [minimum value]

The expression to generate a random number between 1 and 10 would be the following:

( random() * (10-1) ) +1 

or

( random() * 9 ) +1

The expression above will provide a decimal result you will need to round for an integer result:

roundxl( ( random() * 9 ) +1 , 0 )  //Excel-style rounding


Determining the number of values in a given range

The number of values in a given range is determined as:

(max value) - (min value) + 1

This can be deceiving at first glance. For example, the range between 20 and 40 includes 21 values.


Examples

Extra spaces added for clarity.

random() //Returns 0.37130572949209748204
( random() * 10 ) +2   //Random number between 2 and 12, unrounded.
roundxl( ( random() * 20 ) +20 , 0 )  //Random number between 20 and 40, rounded to an integer.


See also

syntax/functions/random.txt · Last modified: 2021/10/15 14:56 by craigt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki