Math.random
From codeTank
Contents |
Description
math.random will generate a pseudo-random number. It has the same behavior as Lua's default math.random function, although it is implemented differently.
Arguments
math.random() -- return a random number N, such that 0 >= N > 1 math.random(max) -- return a random integer N, such that 1 >= N >= max math.random(min, max) -- return a random integer N, such that min >= N >= max
math.random takes zero, one, or two arguments. If no arguments are provided, it will return a pseudo-random number between 0 and 1 (including 0, but not including 1). If one argument, max, is provided, it will return a pseudo-random integer between 1 and max (including 1 and max). If two arguments, min and max, are provided, it will return a pseudo-random integer between min and max (including min and max).
Returns
math.random will return one pseudo-random number. See above for further explanation.
Example
print(math.random()) -- outputs: 0.12350893494668 print(math.random()) -- outputs: 0.74613358371148 print(math.random(3258)) -- outputs: 1337 math.randomseed(1234) -- seeds random number generator with 1234 print(math.random(7)) -- outputs: 6 print(math.random(3)) -- outputs: 2 math.randomseed() -- seeds random number generator with high resolution system clock print(math.random(4, 10)) -- outputs a number between 4 and 10 (inclusive) print(math.random(12, 13)) -- outputs either 12 or 13
Special Notes
math.random was provided by Lua, but was overwritten by Brain Damage. The reason I chose to rewrite math.random was to provide a mechanism for seeding the RNG based on the system time in milliseconds, and to use a better RNG algorithm. Currently, Brain Damage uses the SIMD oriented Fast Mersenne Twister (SFMT) pseudo-random number generator.

