Random numbers, on device vs simulator

My game needs (pseudo) random numbers to randomize things like which type of enemy appears, which type of item drops, etc. 

In the simulator on my Mac , the following works as expected: 

math.randomseed( socket.gettime() \* 10000 ) local rand = math.random()

Run about once a second, it prints out:

0.32544071754694 0.088621043175748 0.85180136880456 0.56267938928803 0.50969980820534 0.08904004054565

But when I run the same exact code on either of my Android devices (Nexus 7, Moto X), the output is always the same, no matter what I do. Even after a rebuild and re-install, it generates the exact same number on every iteration:

0.54531151640476

So I’m guessing it’s something with socket.getTime() returning the same value no matter what? If so, any suggestions for a better way to generate a seed that’s more granular than every second?

There’s only a need to seed once per app start, so I don’t see a problem with math.randomseed( os.time() ) …

Time has no bearing on how fast or how granular random numbers are.  Time is used as a seed to create a random starting point (i.e. the numbers don’t repeat over and over).   But once seeded you can get the random numbers as fast as you can possibly call them.

The symptom you are seeing seems to be the socket.gettime() perhaps not returning a granular enough value that when you multiple it by 10000 you’re possibly just getting 0 or the same number over and over.    Using os.time() with no additional math will work.  You don’t need to make the seed any more random than that.  It’s just creating a starting point and a 1 second difference is enough.  The chance that two people will start your app in the exact same second is very slim. 

Rob

Thanks guys. Sounds like I misunderstood the purpose of seeding the random number generator. Instead of setting the seed prior to each call of math.random() I’ll set it once at the top of main.lua. 

There’s only a need to seed once per app start, so I don’t see a problem with math.randomseed( os.time() ) …

Time has no bearing on how fast or how granular random numbers are.  Time is used as a seed to create a random starting point (i.e. the numbers don’t repeat over and over).   But once seeded you can get the random numbers as fast as you can possibly call them.

The symptom you are seeing seems to be the socket.gettime() perhaps not returning a granular enough value that when you multiple it by 10000 you’re possibly just getting 0 or the same number over and over.    Using os.time() with no additional math will work.  You don’t need to make the seed any more random than that.  It’s just creating a starting point and a 1 second difference is enough.  The chance that two people will start your app in the exact same second is very slim. 

Rob

Thanks guys. Sounds like I misunderstood the purpose of seeding the random number generator. Instead of setting the seed prior to each call of math.random() I’ll set it once at the top of main.lua.