Page 1 of 1

[0.17.34] LuaRandomGenerator seed not working

Posted: Sat Apr 27, 2019 7:04 pm
by boskid
what i did:

Code: Select all

/c
    for seed=100,110 do
        s="with seed "..seed..": ";
        myRng=game.create_random_generator(seed);
        myRng.re_seed(seed)
        for i=1,50 do s=s..myRng(10,99)..","; end
        game.player.print(s)
    end
notes:
- for posting purpose, game.player.print() was changed to log();
- same behavior with re_seed(seed) and with only create_random_generator(seed)

what happend:
- all small seeds produce same sequence

Code: Select all

with seed 100: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 101: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 102: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 103: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 104: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 105: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 106: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 107: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 108: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 109: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 110: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
- seeds up to 341 produce same sequence, then sequence changes but least significant bit is ignored

Code: Select all

with seed 334: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 335: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 336: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 337: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 338: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 339: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 340: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 341: 10,39,93,12,77,69,42,56,90,74,53,82,85,11,52,36,55,19,56,47,29,10,77,94,25,75,15,70,79,72,10,15,49,63,41,89,97,17,68,74,71,68,95,29,74,21,30,66,97,60,
with seed 342: 10,40,93,57,56,69,87,80,79,23,32,82,40,37,42,86,81,63,12,24,18,61,87,94,70,84,25,28,70,29,56,48,37,10,63,67,52,88,78,12,96,21,51,53,65,73,41,69,99,91,
with seed 343: 10,40,93,57,56,69,87,80,79,23,32,82,40,37,42,86,81,63,12,24,18,61,87,94,70,84,25,28,70,29,56,48,37,10,63,67,52,88,78,12,96,21,51,53,65,73,41,69,99,91,
with seed 344: 10,40,93,12,37,67,88,13,22,85,94,82,78,97,66,24,31,11,57,78,86,44,95,93,24,77,55,71,75,69,15,34,69,35,88,93,98,70,99,14,28,69,75,72,34,42,40,72,78,88,
with seed 345: 10,40,93,12,37,67,88,13,22,85,94,82,78,97,66,24,31,11,57,78,86,44,95,93,24,77,55,71,75,69,15,34,69,35,88,93,98,70,99,14,28,69,75,72,34,42,40,72,78,88,
with seed 346: 10,41,93,57,15,67,43,36,11,35,72,82,33,72,56,75,52,55,13,55,97,95,61,93,69,70,68,28,83,25,59,24,58,83,20,71,53,44,64,76,47,26,30,94,48,82,28,74,79,74,
with seed 347: 10,41,93,57,15,67,43,36,11,35,72,82,33,72,56,75,52,55,13,55,97,95,61,93,69,70,68,28,83,25,59,24,58,83,20,71,53,44,64,76,47,26,30,94,48,82,28,74,79,74,
with seed 348: 10,42,93,12,80,67,88,59,45,97,50,82,78,48,91,13,69,14,59,34,63,33,29,93,24,17,80,65,48,66,13,56,93,52,41,47,97,31,29,48,57,67,77,28,17,50,61,66,81,15,
with seed 349: 10,42,93,12,80,67,88,59,45,97,50,82,78,48,91,13,69,14,59,34,63,33,29,93,24,17,80,65,48,66,13,56,93,52,41,47,97,31,29,48,57,67,77,28,17,50,61,66,81,15,
expected:
- more dramatic effect with small seeds (<342)
- least significant bit not ignored

Re: [0.17.34] LuaRandomGenerator seed not working

Posted: Sat Apr 27, 2019 8:22 pm
by Rseding91
Thanks for the report. All seed values <= 341 will produce the same results. That has nothing to do with re_seed.

Re: [0.17.34] LuaRandomGenerator seed not working

Posted: Sun Apr 28, 2019 12:54 am
by DaveMcW
LuaRandomGenerator documentation should be updated to note that the least significant bit is ignored and all seed values <= 341 will produce the same results.

Re: [0.17.34] LuaRandomGenerator seed not working

Posted: Sun Apr 28, 2019 8:29 am
by boskid
For me, "default expectation" for PRNG is that every seed produces different deterministric sequence.

Possible solutions for this case are:
- update documentation of LuaRandomGenerator so it clearly states that seeds <=341 produce same sequence and least significant bit is ignored
- OR to include formula "340+2*seed" that compensated for this issue making this generator really produce different sequences for different seed (at least until this formula overflows)
- OR to change random generator implementation to one not affected with "weak seed values" (even simple LFSR with 32 bits would be better [zero seed would need to be handled differently])

Re: [0.17.34] LuaRandomGenerator seed not working

Posted: Sun Apr 28, 2019 5:39 pm
by Rseding91
We're not going to be changing the PRNG at this point because it would break every single maps terrain generation out there if it was changed.