clearing seed states for math.random and MtRand

Posted by Victorious on Fri 13 Dec 2013 12:44 PM — 7 posts, 30,409 views.

#0
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.
Australia Forum Administrator #1
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?
#2
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
Australia Forum Administrator #3
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
#4
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()
Amended on Sat 14 Dec 2013 02:54 PM by Victorious
Australia Forum Administrator #5
The high-performance timer will increment very quickly, and is probably a good random seed.
#6
Thanks, using a timer now that seeds it periodically.