Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ clearing seed states for math.random and MtRand

clearing seed states for math.random and MtRand

Posting of new messages is disabled at present.

Refresh page


Posted by Victorious   (89 posts)  Bio
Date Fri 13 Dec 2013 12:44 PM (UTC)
Message
Is there a way to ensure that the random numbers being generated has not been affected by seeds? e.g seeding math.random and MtRand in the world file causes randomly generated numbers to be generated off the seed even if the code for doing so is in a plugin file. Have tried having my main number generating function calling another dummy function that generates random numbers in hopes that it clears the seed state but it didn't work.
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 13 Dec 2013 08:08 PM (UTC)
Message
Each plugin has its own script space so that shouldn't happen. Can you post a bit of code that demonstrates what you are talking about?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #2 on Sat 14 Dec 2013 06:31 AM (UTC)
Message
have an alias in the world file do a MtSrand(12345), as well as testing using the /prefix to execute it.

The code in my plugin is:

<alias
name="Rand"
match="rand"
enabled="y"
send_to="12"
sequence="100"
>
<send>
print(MtRand())
print(MtRand())
print(MtRand())
</send>
</alias>

Executing this alias gets this every time after seeding in the world file:
0.13070729846322
0.039759489654285
0.82643612033964

Also tested with math.random. Had an alias in the world file do a math.randomseed(12345) and this is the corresponding code in the plugin file
<alias
match="rand2"
enabled="y"
send_to="12"
sequence="100"
>
<send>
print(math.random(1, 10))
print(math.random(1, 10))
print(math.random(1, 10))
</send>
</alias>

first 3 numbers after seeding are always:
3
6
8
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 14 Dec 2013 11:12 AM (UTC)
Message
MtRand is a world function, not a Lua function, so its effect will be global across all worlds.

As for math.randomseed, it appears that although each world has its own script space, Lua's random function calls the C random function which is function shared across all script spaces. Thus what you observe would indeed happen.

I'm not sure if you are going for randomness or repeatability here.

(Pseudo) random number generators are pretty heavily documented. All I can suggest is writing your own in Lua if you want to have more control over it.


Wikipedia: Random number generation

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #4 on Sat 14 Dec 2013 02:52 PM (UTC)

Amended on Sat 14 Dec 2013 02:54 PM (UTC) by Victorious

Message
Hm, maybe I could find some good seeds that I can use myself, which would eliminate the effects of any potential prier seeds. Which of the following would be good sources of numbers to use? Planning to have some mathematical operations on some/all of these to generate a hopefully good seed.

os.time, os.clock, collectgarbage("count"), GetInfo(232) [windows high-performance] timer, GetReceivedBytes(), GetNotePadLength, GetLinesInBufferCount(), GetConnectDuration(), GetScriptTime(), GetSentBytes(), WorldPort()
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #5 on Sun 15 Dec 2013 05:34 AM (UTC)
Message
The high-performance timer will increment very quickly, and is probably a good random seed.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #6 on Sun 15 Dec 2013 06:32 AM (UTC)
Message
Thanks, using a timer now that seeds it periodically.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,269 views.

Posting of new messages is disabled at present.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.