[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  clearing seed states for math.random and MtRand
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

clearing seed states for math.random and MtRand

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Victorious   (85 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] 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()
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by Victorious   (85 posts)  [Biography] bio
Date Reply #6 on Sun 15 Dec 2013 06:32 AM (UTC)
Message
Thanks, using a timer now that seeds it periodically.
[Go to top] 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.


4,146 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]