Issue information

Issue ID
#8315
Status
Confirmed
Severity
None
Started
Ancyker
Sep 2, 2014 0:02
Last Post
csnv
Sep 14, 2014 16:40
Confirmation
Yes (1)
No (2)

Ancyker - Sep 2, 2014 0:02

So, as any large server can tell you, random isn't very random (0.20% drop chance, sure):

[img]http://i.imgur.com/Ynzrpv8.png[/img]

I used to just blame this on the fact there are a lot of players and pseudorandom number generators just not being that good. I ran off to find a true random number generator and then began checking the source code to find out if it'd be possible to implement it. The most effective way would have been to just edit the function that plugs in the seed.

Then I discovered something interesting:

It appears the seed for the random number generator, which is set by rnd_init(), is only called once at boot time (in core.c, by main()). This means that the seed is never changed and that "random" numbers are very predictable after any decent length of up time.

This should be modified so the seed is changed quite often (I'd say at longest every few minutes). It'd also be nice if it would be seeded with some data from /dev/urandom or /dev/random instead of [b]the[/b] [b]time [/b](really?).

This post has been edited by Ancyker on Sep 2, 2014 0:15

Haru - Sep 13, 2014 19:05

Further examination showed evidence that the source of the issue isn't on the random number generator (nor its seed), but rather, custom code.

Even so, I agree that we could improve the random number generator in the following way:[list]
[*]Offer a set of compile time options to use a system-provided HRNG (or PRNG) instead of the time, to seed the Mersenne Twister.
[list]
[*]#define SEED_WITH_SYSTEM_RNG
When not defined, the current time is used to seed the MT.
When defined, a system-provided random number generator is used to seed the MT. This is probably /dev/urandom on POSIX-compliant systems. See your UNIX manual on how to use /dev/urandom (hint: fopen, fread 4 bytes to an int variable, fclose). Bonus points if the implementation includes a Windows equivalent, else it can simply yield an #error there.
[*]#define SYSTEM_RNG_PATH "/dev/urandom"
Lets the user choose a different PRNG (or a HRNG), would they wish so.
[/list][*]Offer an option to re-seed the MT after <n> generated numbers. This could be implemented with a counter in random.c, and a re-seed is issued when the counter exceeds a certain value. This can be done with another compile-time option.
[*]All the above options should be disabled by default, for performance reasons.
[/list]

csnv - Sep 14, 2014 16:40

Adding to this, we could update our Mersenne Twister code with the updated version, which is supossed to be more efficient and have better performance [url="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html"]http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html[/url]