Mtasa-blue: Random's consistency

Created on 11 Jun 2019  路  5Comments  路  Source: multitheftauto/mtasa-blue

Is your feature request related to a problem? Please describe.
As I understand, Lua's math.random depends on OS' implementation of them.
At the moment math.random returns different values even with same seed (server and client runned on different platforms). So it can't be used for world generation, etc (I have to send huge amount of data from server to clients or use 3rd party libraries)

Describe the solution you'd like
Add own random functions which will use same algorithm on each platform.

Describe alternatives you've considered
3rd party libraries written on Lua may significantly reduce the perfromance if it used a lot

Additional context
no

enhancement

All 5 comments

Interesting...
Lua likely depends on the C version of rand(), so probably using C++'s rand() would yield the same results.
Btw, https://love2d.org/forums/viewtopic.php?t=84496 they say that L脰VE has a consistent PRNG:

Yeah, that's a given; it's also consistent across all platforms and Lua interpreters (while LuaJIT packs a consistent PRNG, L脰VE can also use standard Lua).

This is love's random generator:

  • underlying impl: RandomGenerator dot cpp, h
  • lua defs: wrap_RandomGenerator dot cpp, h, lua

We don't need to reinvent the wheel here. C++ has had <random> since C++11. We could simply expose a std::mt19937 instance to Lua. That would also give us deterministic behavior as mt19937 is strictly defined.

Things to discuss:

  • Do we want a single PRNG for everything (direct replacement of rand())?
  • Do we want a PRNG for each individual resource (this would avoid side-effects of different resources interacting via the PRNG internal state)
  • Do we want scripts to be able to create PRNG instances (might be cool for random numbers synchronized across clients)
  • Should we replace the default math.random or should we introduce our own function name?

Do we want scripts to be able to create PRNG instances (might be cool for random numbers synchronized across clients)

I think this would be good. This would also be beneficial for multi-gamemode servers.

replace default

As long as we can create RandomGenerator instances, I don't see much benefit to having our own equivalent to math.random (or replacing the default math.random).

Imo the only benefit would be making it slightly easier to use, but if your code depends on a consistent PRNG, you probably wouldn't mind going through the minor inconvenience of instantiating your own generator.

global vs. per-resource PRNG

If we do intend to have a simple function (without the need of instantiating a generator):

  • if we replace math.random, we may want to just use a single PRNG for everything

    • this would ensure that behaviour between existing resources will stay the same, even though the same seed will now generate different values

  • if we introduced our own (differently named) math.random function, I think _per-resource_ would be good

Per-resource PRNG, please.
Otherwise the result may be different (especially if https://github.com/multitheftauto/mtasa-blue/issues/918 implemented)
upd: does not matter if instances used

Was this page helpful?
0 / 5 - 0 ratings