Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random's consistency #991

Open
WADmitry opened this issue Jun 11, 2019 · 5 comments
Open

Random's consistency #991

WADmitry opened this issue Jun 11, 2019 · 5 comments
Labels
enhancement New feature or request
Projects

Comments

@WADmitry
Copy link

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

@WADmitry WADmitry added the enhancement New feature or request label Jun 11, 2019
@WADmitry WADmitry changed the title random consistency random's consistency Jun 15, 2019
@Pirulax
Copy link
Contributor

Pirulax commented Jun 16, 2019

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).

@qaisjp
Copy link
Contributor

qaisjp commented Jun 16, 2019

This is love's random generator:

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

@sbx320
Copy link
Member

sbx320 commented Jun 17, 2019

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?

@qaisjp
Copy link
Contributor

qaisjp commented Jun 17, 2019

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

@WADmitry
Copy link
Author

WADmitry commented Jun 18, 2019

Per-resource PRNG, please.
Otherwise the result may be different (especially if #918 implemented)
upd: does not matter if instances used

@patrikjuvonen patrikjuvonen changed the title random's consistency Random's consistency Jun 18, 2019
@qaisjp qaisjp added this to Incoming in Proposals via automation Mar 15, 2020
@qaisjp qaisjp moved this from Incoming to Accepted in Proposals Mar 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Proposals
  
Accepted
Development

No branches or pull requests

4 participants