[Switch] Lua random ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.