# Function Reference

Random

Generates a pseudo-random float-type number.

Random ( [Min [, Max [, Flag]]] )

Parameters

Min | [optional] The smallest number to be generated. The default is 0.
Max | [optional] The largest number to be generated. The default is 1.
Flag | [optional] If this is set to 1 then an integer result will be returned. Default is a floating point number.

Return Value

Success: | Returns a pseudo-random number between Min and Max.
Failure: | Returns 0 and sets @error flag to 1 if bad parameters.

Remarks

By default the Random function works with decimal/floating point numbers. If you want to only have integer/whole number results then set the Flag to 1.
If only one argument is provided, then it is interpreted to be the Max.
The result will be in the range of Min to Max INCLUSIVE when using integers (just short of Max when using floats).
Comments based on the original source
This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura, Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.
The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 219937-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. For more information see the inventors' web page at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Related

Int, Round

Example

;Flip of coin
If Random() < 0.5 Then ; Returns a value between 0 and 1.
$msg = "Heads. 50% Win"
Else
$msg = "Tails. 50% Loss"
Endif
MsgBox(0,"Coin toss", $msg )
;Roll of a die
msgBox(0, "Roll of die", "You rolled a " & Random(1, 6, 1) )
$StockPrice = 98
;In the middle of a stock market simulation game
$StockPriceChange = Random(-10, 10, 1) ; generate an integer between -10 and 10
$StockPrice = $StockPrice + $StockPriceChange
If $StockPriceChange < 0 Then
MsgBox(4096, "Stock Change", "Your stock dropped to $" & $StockPrice)
ElseIf $StockPriceChange > 0 Then
MsgBox(4096, "Stock Change", "Your stock rose to $" & $StockPrice)
Else
MsgBox(4096, "Stock Change", "Your stock stayed at $" & $StockPrice)
Endif
;Random letter
If Random() < 0.5 Then
;Capitals
$Letter = Chr(Random(Asc("A"), Asc("Z"), 1))
Else
;Lower case
$Letter = Chr(Random(Asc("a"), Asc("z"), 1))
Endif