randomseed()
In Lua, the math.randomseed()
function is a standard function that is part of the Lua math
library. The math.randomseed()
function initializes the random number generator with a specific seed value. When the random number generator is initialized with a seed value, it ensures that the sequence of random numbers generated is predictable and reproducible.
This is particularly useful in applications such as simulations or games, where a specific random sequence can be recreated for debugging or sharing results.
Syntax
math.randomseed(seed)
This randomseed()
method of the Lua math
library, takes an argument seed
which can be any integer value.
Example 1
Seed with a Fixed Value
In this example, by seeding the random number generator with the value 42
, it ensures that the subsequent calls to math.random()
will always yield the same result.
math.randomseed(42) -- Seed the random number generator with 42print("Random Number: ", math.random()) -- This will always produce the same random number
This example results in the following output:
Random Number: 0.93081217803957
Example 2
Creating Reproducible Random Sequences
In this example, by using the same seed
value 1234
, it will generate the same sequence of 5 random numbers every time.
math.randomseed(1234) -- Seed with a specific valuefor i = 1, 5 doprint("Random Number ", i, ": ", math.random(1, 10)) -- Generate and print 5 random numbersend
This example results in the following output:
Random Number 1 : 3Random Number 2 : 7Random Number 3 : 1Random Number 4 : 4Random Number 5 : 9
Example 3
Using System Time as a Seed
In this example, by using os.time()
, a different seed
value is taken each time the Lua script is executed, leading to more unpredictable random sequences. This is a common technique to make random sequences less predictable.
print("System Time:", os.time())math.randomseed(os.time()) -- Seed with the current system timeprint("Random Number:", math.random(1, 100))
This code will result in an output similar to the following:
System Time: 1697258566Random Number: 53
Note: Using this technique for cryptographic purposes is not advisable, as the time value from the
os.time()
is hardly a secret.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours