math.random()

Type Function
Library math.*
Return value Number
Revision 2014.2438
Keywords random, random number
See also math.randomseed()

Overview

Returns a pseudo-random number from a sequence with uniform distribution.

This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. No guarantees can be given for its statistical properties.

There are 3 versions of this function:

  1. When called without arguments, returns a uniform pseudo-random real number in the range [0,1].
  2. When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m].
  3. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].

Syntax

math.random()

math.random( m )

math.random( m, n )
m (optional)

Number. A number.

n (optional)

Number. A number.

Examples

print(math.random())        ---> a number between 0 and 1
print(math.random())        ---> another number between 0 and 1
print(math.random(10))      ---> an integer between 1 and 10 (inclusive)
print(math.random(70, 80))  ---> an integer between 70 and 80 (inclusive)