📜  math randomseed lua (1)

📅  最后修改于: 2023-12-03 15:17:33.435000             🧑  作者: Mango

math.randomseed() in Lua

The math.randomseed(seed) function in Lua sets the seed for the random number generator. The random number generator generates pseudo-random numbers based on the seed value. This function is often used at the beginning of a program to ensure different results each time the program is run, or to reproduce the same sequence of random numbers.

Syntax
math.randomseed(seed)
Parameters
  • seed (optional): A number that will be used as the seed for the random number generator. If no seed is provided, it will be initialized with a default seed based on the system time.
Return Value

The math.randomseed() function does not return any value.

Example
-- Set the seed to a specific value
math.randomseed(1234)

-- Generate and print 5 random numbers
for i = 1, 5 do
    print(math.random())
end

Output:

0.96645353569232
0.44073259917537
0.0074914700582142
0.31876753934008
0.4447135475366
Explanation

In the example above, the math.randomseed(1234) call sets the seed for the random number generator to 1234. As a result, the sequence of random numbers generated by math.random() will always be the same when this code is executed.

Note: If you don't provide a seed, Lua automatically initializes the random number generator with a different seed value each time the program runs.