Bug? Android Random Number Problems (using the same seed)

Hey everyone,

I’m having issues with generating random numbers on Android. Even when doing math.randomseed(*myseedhere*) and then doing math.random(), I get different results each time, even though the exact same seed is being used. This bug does not happen on the simulator or any iOS device. Those devices always report the same numbers when using the same seed each time.

I’ve tested this on 2 Android devices (Galaxy Tab and a Galaxy Nexus), and it still happened on both. Has anyone else had this problem, or have a fix for this bug?

Thanks [import]uid: 51654 topic_id: 25196 reply_id: 325196[/import]

I am experiencing this as well (Galaxy S2, Corona 2012.840). To test this, I wrote the following program:

[lua]math.randomseed(12345)
–generate some random numbers to make sure not only the first, but also the upcoming numbers are properly randomised.
for i = 1, 100 do
print(math.random(1,100))
end
local myText = display.newText( math.random(1,100), 0, 0, native.systemFont, 40 )
myText.x = display.contentWidth / 2
myText.y = display.contentWidth / 2
myText:setTextColor( 255,110,110 )[/lua]

However, with this program, everything worked as it should. The numbers was the same every time I ran the program (even though the numbers was not the same as on Windows and iOS devices).

In my real application however, I get different random numbers every time I run it, even if math.randomseed is set as above and only once (always before math.random is called). This problem does only occur on Android. There must be something in the code making math.random behaving unintentionally, but I can not see the logical explanation to what and why this is happening.

Another issue I have with math.random is that the random numbers on Windows simulator are different than those from Xcode and iOS. Note that the random numbers on the Windows simulator are the same when relaunching the application, unlike the issue I explained above.

Edit: Mentionable that I am using Android 4.0.3. [import]uid: 91236 topic_id: 25196 reply_id: 113391[/import]

Don’t have a real answer but below is some info that may at least help!

The math.random() function isn’t so much as part of the CoronaSDK API as it is just native Lua(which was built on c rand). if you do some googling on the lua math.random or, if you feel like a headache, on the c rand() function you will find quite a bit of info and various solutions/work around for various issues. You might find your answer there but most people have more issues with the function not being random enough, not the other way around :slight_smile:

Different OSes end up producing different numbers despite the randomseed that is set, which is actually notated in Lua’s documentation. If I had the issues above and it was holding me up I would just create the sequence myself. If it was a rather long one that I may use the random function to create it initially and then put it in a hard typed array/table /shrug. [import]uid: 147305 topic_id: 25196 reply_id: 113396[/import]