User Tools

Site Tools


syntax:functions:random

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
syntax:functions:random [2016/06/20 11:57] dmitrysyntax:functions:random [2021/10/15 14:56] (current) – Add See also section. craigt
Line 1: Line 1:
-===== Random() =====+====== Random() =====
 +Category:  Number function
  
-Random number between 0 and 1.+\\  
 +=====Description===== 
 +This function generates a random number between 0 and 1.
  
-^  Example  ^  Result  ^ +\\  
-| random()  |  0.37130572949209748204 |+=====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:randbetween|RandBetween(min_num, max_num)]]
syntax/functions/random.1466438271.txt.gz · Last modified: 2016/06/20 11:57 by dmitry

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki