Is there any way that you could make math.random() generate the same random numbers on Android and iOS when given the same seed?
+1
May not be what you looking for or want to do but maybe generate random numbers on a server and download to devices on first run.
Exactly what I was planning to do! … but It’d be nice not having to go through such an ordeal.
Could you generate a huge list of random numbers and store them in a file in the build negating the need for the server? Or do they need to be different for each user?
just make your own. would you prefer “simple/fast/lo-quality” or “complex/slow/hi-quality”? google for “linear congruential generator native lua” or “mersenne twister native lua”
@GlitchGames
That would work in some projects however my current project needs the random number seed to change on a daily basis, and they should generate the same numbers on Android / iOS.
Looks very promising. Thanks!
I’ve decided to have a closer look at this native Lua library (MIT license).
fwiw, since not knowing your background, here’s what i like to do, so can trade in whatever implementation without vast code rewrites:
math.original\_randomseed = math.randomseed math.randomseed = function(x) print("handle randomseed 1 arg form") end math.original\_random = math.random math.random = function(m,n) if (not m) then print("handle random 0 arg form") else if (not n) then print("handle random 1 arg form") else print("handle random 2 arg form") end end end
http://developer.coronalabs.com/code/portable-seedable-random-number-generator
I’ve always used this code without any problems.
Huh! I can’t believe I missed that when searching the Shared Code database!
Thanks @nick for the link!
I’m gonna risk tossing this out there in the spirit of a “public service announcement”, not intending to come across as a jerk just for criticizing someone else’s code, but…
Every game may have its own unique needs for random numbers, and not all PRNG’s are created equal. For example, what might be appropriate for a kids’ “flip-match” game might not be appropriate for shuffling cards in a gambling game (where some proxy for real money might be involved). So, choose wisely.
The generator linked above has some not-insignificant limitations. (or, more bluntly: it’s quite poor at simulating true randomness) You could do far better even with a simple “one-liner” LCG.
I know of which I speak, but I wouldn’t expect anyone to take my word for it – do your own due diligence as suits your own particular application. This is a well-studied field, and there are many known implementations with known statistical properties, at least one of which is likely to be suitable.
It’s good advice.
For my particular situation it’s only for calculating positions of objects on screen and they need to be the same on Android/iOS, so simulating true randomness isn’t a requirement.
+1
May not be what you looking for or want to do but maybe generate random numbers on a server and download to devices on first run.
Exactly what I was planning to do! … but It’d be nice not having to go through such an ordeal.
Could you generate a huge list of random numbers and store them in a file in the build negating the need for the server? Or do they need to be different for each user?
just make your own. would you prefer “simple/fast/lo-quality” or “complex/slow/hi-quality”? google for “linear congruential generator native lua” or “mersenne twister native lua”
@GlitchGames
That would work in some projects however my current project needs the random number seed to change on a daily basis, and they should generate the same numbers on Android / iOS.
Looks very promising. Thanks!
I’ve decided to have a closer look at this native Lua library (MIT license).
fwiw, since not knowing your background, here’s what i like to do, so can trade in whatever implementation without vast code rewrites:
math.original\_randomseed = math.randomseed math.randomseed = function(x) print("handle randomseed 1 arg form") end math.original\_random = math.random math.random = function(m,n) if (not m) then print("handle random 0 arg form") else if (not n) then print("handle random 1 arg form") else print("handle random 2 arg form") end end end
http://developer.coronalabs.com/code/portable-seedable-random-number-generator
I’ve always used this code without any problems.