Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Same random numbers for Android / iOS
Started by ingemar Feb 26 2014 06:14 AM

13 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

@CoronaLabs

 

Is there any way that you could make math.random() generate the same random numbers on Android and iOS when given the same seed?



[TOPIC: post.html]
#2

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Pro
  • PipPipPipPipPipPip
  • 1,921 posts
  • Jedi


[TOPIC: post.html]
#3

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Pro
  • PipPipPipPipPipPip
  • 1,921 posts
  • Jedi

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.

[TOPIC: post.html]
#4

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

;)  Exactly what I was planning to do!  ... but It'd be nice not having to go through such an ordeal.



[TOPIC: post.html]
#5

Glitch Games

[GLOBAL: userInfoPane.html]
Glitch Games
  • Pro
  • PipPipPipPipPipPip
  • 421 posts
  • Jedi

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?



[TOPIC: post.html]
#6

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Basic
  • PipPipPipPipPipPip
  • 219 posts
  • Jedi

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"

[TOPIC: post.html]
#7

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

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

 

@dave

Looks very promising. Thanks! 

I've decided to have a closer look at this native Lua library (MIT license).

http://love2d.org/forums/viewtopic.php?f=5&t=3424



[TOPIC: post.html]
#8

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Basic
  • PipPipPipPipPipPip
  • 219 posts
  • Jedi

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


[TOPIC: post.html]
#9

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Pro
  • PipPipPipPipPipPip
  • 656 posts
  • Jedi


[TOPIC: post.html]
#10

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

Huh!  I can't believe I missed that when searching the Shared Code database!

Thanks @nick for the link!



[TOPIC: post.html]
#11

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Basic
  • PipPipPipPipPipPip
  • 219 posts
  • Jedi

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.

[TOPIC: post.html]
#12

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

@dave

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.



[TOPIC: post.html]
#13

atrizhong

[GLOBAL: userInfoPane.html]
atrizhong
  • Starter
  • PipPip
  • 29 posts
  • Member

Would this work

math.randomseed( os.time() )



[TOPIC: post.html]
#14

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Enterprise
  • PipPipPipPipPipPip
  • 1,966 posts
  • Jedi

No, the problem is that Android and iOS have different algorithms to generate random numbers.

 

I ended up using this library:

http://love2d.org/forums/viewtopic.php?f=5&t=3424




[topic_controls]
[/topic_controls]