Let's take a look at the two codes:
①
Math. randomseed (OS. Time ())
For I = 1, 10 do
N = math. Random (10)
Print (N)
The running result is:
6
3
2
10
7
5
4
3
4
10
②
For I = 1, 10 do
Math. randomseed (OS. Time ())
N = math. Random (10)
Print (N)
End
The running result is:
6
6
6
6
6
6
6
6
6
6
Conclusion: Multiple math operations are required. random () gets different results. Math is not allowed. randomseed () and it are written into a loop. It is best not to write it into a loop, but as initialization data.
Let's take a look at code ①. After multiple runs, we find that all the first results are 6. That is to say, the random function of Lua, the first value randomly generated each time, is unreliable, if we want to use a random function, we 'd better discard the first value, starting from the second result.
This also confirms the above conclusion that writing seed into a loop with random is the first execution, so the value is the same.
[Switch] Lua random ()