Math.randomseed

From codeTank

Jump to: navigation, search

Contents

Description

math.randomseed will seed the random number generator (RNG). The initial seed value of every program is 0 upon execution.

Arguments

math.randomseed() -- will seed the RNG with the system clock (millisecond resolution)
math.randomseed(seed) -- seed the RNG with the seed value

math.randomseed can take zero or one argument. If no arguments are specified, then it will seed the random number generator (RNG) with the system time in milliseconds. This is the preferred method of seeding the RNG if you just want some different random numbers every time you execute your script. Otherwise, you can specify the seed value, which will ensure a consistent sequence of random numbers, given the same seed.

Returns

math.randomseed does not return anything.

Special Notes

math.randomseed was provided by Lua, but was overwritten by Brain Damage. The reason I chose to rewrite math.randomseed 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.

Example

-- initial seed of every program is 0
print(math.random()) -- outputs: 0.12350893494668
math.randomseed(0)
print(math.random()) -- same output as above

math.randomseed(90210)
print(math.random()) -- outputs: 0.29928496842715

math.randomseed() -- seed RNG using system time in milliseconds
print(math.random()) -- output virtually unpredictable

See Also

Personal tools