Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
did you notice math.random always produce equal sequences of numbers on iOS — Gideros Forum

did you notice math.random always produce equal sequences of numbers on iOS

alexzhengalexzheng Guru
edited May 2012 in General questions
I am not sure from which iOS version this begins, my iOS version is 5.1.1.
I remember it produce different number when my device was 4.*.
However,just now,I found it always produce the same sequences of number during each application lifetime,
for example,if it produces 1, 2, 3,it will still produces 1,2,3 when the app startup next time.
And it produce different number when run in the player both on mac and device.

I almost got crazy until I add math.randomseed(os.time()) when the application startup.


Dislikes: victor_cappa, nelinho

+1 -1 (+0 / -2 )Share on Facebook

Comments

  • That's probably directly hitting the OS random number generator and it's never guaranteed to work properly until you call the randomseed() function.

    However it CAN be useful to seed the generator with a specific value so you can generate the same sequence over and over (especially if your procedurally generating content) - however as you've found out, the results of the function can't be guaranteed from OS version to OS version and if you were going to be basing any elements of your game / gameplay on these values then you'd really need to make your own psuedo random sequence generator so you can be sure the results are consistent across all platforms (and OS versions).

    Good find!
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • It is usual practice to call math.randomseed(os.time()) to set a random seed as you cannot rely on the system being initialised before your code runs.
  • I've used the following from http://lua-users.org/wiki/MathLibraryTutorial
    Why math.random() might give weird results on OSX and FreeBSD?
    *...The problem seems to be that when the seeds differ very little the first value of generated by BSD rand() also differ very little. This difference is lost when Lua converts the integer returned by rand() into a real number, effectively preserving only the high bits in the result. When you call math.random(1,100) from Lua, the low-bit difference vanishes and you see the same integer result.
     
    -- improve seeding on these platforms by throwing away the high part of time, 
    -- then reversing the digits so the least significant part makes the biggest change
    -- NOTE this should not be considered a replacement for using a stronger random function
    -- ~ferrix
    math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
  • Really?.... To have better random we have to reverse the time?
  • @Rickyngk

    interesting...
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • This code looks so strange and blurry, but so amazing it works. I've used without any clues about it. :D

    Likes: avo

    +1 -1 (+1 / -0 )Share on Facebook
  • I encountered the same problem (on OSX) and found a discussion and solution on http://lua-users.org/wiki/MathLibraryTutorial:
    -- Initialize the pseudo random number generator
    math.randomseed( os.time() )
    math.random(); math.random(); math.random()
    -- done. :-)
    There is a more involved solution that supposedly gives better randoms, but this worked for me.

    Regards,
    -Brendan
Sign In or Register to comment.