[Lua] use a random number

Source: Internet
Author: User
Tags random seed

The role created in the game has a random name function and uses a random number. I found an article on the Internet that uses a random number in Lua. Mark it.

 

Two functions are required for Lua to generate a random number:
Math. randomseed (XX), math. Random ([n [, m])

1. Math. randomseed (n) receives an integer N as the random sequence seed.
2. math. random ([n [, m]) can be used in three ways: Call without a parameter to generate a floating point random number between (); only the parameter n produces an integer between 1 and N; there are two parameters n, m, which generate random integers between N-M. For the same random seed, the generated random sequence must be the same. Therefore, it is very important to assign different seeds to each program running. Naturally, the system time is used as the Random Seed, namely:

Math. randomseed (OS. Time ())

---- Then generate random numbers continuously

For I = 1, 5 do

Print (math. Random ())

End

But the problem arises. If you run the program several times in a short period of time, the random sequence you get will be almost unchanged. Like this:
> LUA-e "Io. stdout: setvbuf 'No'" "test. Lua"
0.71141697439497
0.060121463667714
0.067506942960906
0.8607745597705
0.60652485732597
> Exit code: 0
> LUA-e "Io. stdout: setvbuf 'No'" "test. Lua"
0.71141697439497
0.060121463667714
0.067506942960906
0.8607745597705
0.60652485732597
> Exit code: 0
> LUA-e "Io. stdout: setvbuf 'No'" "test. Lua"
0.7115085299234
0.38813440351573
0.6127201147496
0.59511093478195
0.9212927640614
> Exit code: 0

We can see that the random numbers of the first two runs are the same. The reason is OS. time () returns time in seconds, which is not accurate enough. However, random () still has a problem that if seed is small or the seed changes little, the generated random sequence is still very similar. For example:

Math. randomseed (0, 100)

Print (math. Random (1000 ))

Math. randomseed (0, 102)

Print (math. Random (1000 ))


The two given seed values are 100,102, but the first random number generated by random is the same. Therefore, the OS. Time is not very good under the requirement of "running programs multiple times in a short time. But there is no seed generator that is more convenient than the time function. What should I do?
You can do this:

Math. randomseed (tostring (OS. Time (): reverse (): Sub (1, 6 ))


It is to reverse the value string returned by time (the low position changes to the high position), and then take the high position 6 digits. In this way, even if the time change is small, but because the low position changes high, the seed value changes greatly, so that the pseudo-random sequence generation can be better.

 

[Lua] use random number (convert)

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.