Function Reference


Random

Generates a pseudo-random float-type number.

Random ( [Min = 0 [, Max = 1 [, Flag = 0]]] )

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: a pseudo-random number between Min and Max.
Failure: sets the @error flag to non-zero.

Remarks

If only one argument is provided, then this is interpreted to be the Max parameter.

The result will be in the range of Min to Max INCLUSIVE when using integers (just short of Max when using floats).

If Min and Max are the same value then Random() will return that value - the @error flag will not be set in this case

When using integers Max-Min must be less than 2^31.


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

Round, SRandom

Example

Example 1

#include <MsgBoxConstants.au3>

; Flip a coin.
Example()

Func Example()
        If Random(0, 1, 1) Then ; Return an integer between 0 and 1.
                MsgBox($MB_SYSTEMMODAL, "", "The side of the coin was: Heads") ; If the random integer was 1 then heads was thrown.
        Else
                MsgBox($MB_SYSTEMMODAL, "", "The side of the coin was: Tails") ; If the random integer was 0 then tails was thrown.
        EndIf
EndFunc   ;==>Example

Example 2

#include <MsgBoxConstants.au3>

; Roll a die.

Example()

Func Example()
        MsgBox($MB_SYSTEMMODAL, "", "The die landed on number " & Random(1, 6, 1) & ".") ; Return an integer between 1 and 6.
EndFunc   ;==>Example

Example 3

#include <MsgBoxConstants.au3>

; Create a random string of text.

Example()

Func Example()
        Local $sText = ""
        For $i = 1 To Random(5, 20, 1) ; Return an integer between 5 and 20 to determine the length of the string.
                $sText &= Chr(Random(65, 122, 1)) ; Return an integer between 65 and 122 which represent the ASCII characters between a (lower-case) to Z (upper-case).
        Next
        MsgBox($MB_SYSTEMMODAL, "", "The random string of text was: " & $sText) ; Display the string of text.
EndFunc   ;==>Example