Type Function Library math.* Return value Number Revision Release 2022.3683 Keywords random, random number See also math.randomseed()
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 three usage options for this function:
[0,1]
.m
, math.random
returns a uniform [1,m]
.m
and n
, math.random
returns a uniform [m,n]
.The random number generator needs to be started with a seed using math.randomseed() before this function is called or else it will generate the same sequence every time. While this is useful for testing, it is more usual to get a different random sequence each run by using a different seed
math.random() math.random( m ) math.random( m, n )
Number. A number.
Number. A number.
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)