Random is no longer random

I’m seeing a very strange problem that is only occurring on WP and I wanted to check to see if anyone has encountered and solved it. Basically, I can’t get any math.random variables to actually be randomized. Using the simple code below:

local rndTest = math.random(4,100) print(" rndTest = "..rndTest)

and run through Visual Studio Community 2013, I get a value of 4, every time. On Android and iOS devices, I random values in the 4 to 100 range; on WP, just a value of 4.

This has been tested using my own projects, and the Hello World code delivered by Corona. Is anyone else seeing this? Is this something that I caused inadvertently? Does anyone know which bit I need to flip to send it back to random values? 

I wanted to wait to file a bug report before I checked with the forum first. All suggestions welcome!

You need to “seed” the random number generator as documented here…

   http://docs.coronalabs.com/api/library/math/randomseed.html

I recommend that you seed it with the os.time() function.

Joshua, I was just coming back to this to say that I always have math.randomseed(os.time()) called in my main.lua, but it was commented out for this WP branch. Since it was working as normal on my main Android test device, I figured it was a vagary of WP development.

Should have looked closer. Sorry to bother!

No problem!  :slight_smile:

I haven’t checked for myself, but the random number generator on Android and iOS *might* be already seeded by Google or Apple’s UI framework before it gets to your Lua script.  That’s just a guess, but it’s entirely possible.  It is a global C API afterall.

I can’t confirm this, but I suspect that on OS-X at least, when the simulator starts up, it’s picking up a seemingly randomized seed already.  But when people to go device, they start getting the same set of numbers, like the randomizer is being seeded with 0 on a fresh start of the app.  Given how iOS sandboxs things, I can see this happening. 

Rob

On my end, I was running through supported APIs and commented out an unsupported block, which included my randomseed line. Definitely a PEBKAC. It does serve as a reminder that, just because it’s working on one OS, doesn’t mean it’s going to work on another!

You need to “seed” the random number generator as documented here…

   http://docs.coronalabs.com/api/library/math/randomseed.html

I recommend that you seed it with the os.time() function.

Joshua, I was just coming back to this to say that I always have math.randomseed(os.time()) called in my main.lua, but it was commented out for this WP branch. Since it was working as normal on my main Android test device, I figured it was a vagary of WP development.

Should have looked closer. Sorry to bother!

No problem!  :slight_smile:

I haven’t checked for myself, but the random number generator on Android and iOS *might* be already seeded by Google or Apple’s UI framework before it gets to your Lua script.  That’s just a guess, but it’s entirely possible.  It is a global C API afterall.

I can’t confirm this, but I suspect that on OS-X at least, when the simulator starts up, it’s picking up a seemingly randomized seed already.  But when people to go device, they start getting the same set of numbers, like the randomizer is being seeded with 0 on a fresh start of the app.  Given how iOS sandboxs things, I can see this happening. 

Rob

On my end, I was running through supported APIs and commented out an unsupported block, which included my randomseed line. Definitely a PEBKAC. It does serve as a reminder that, just because it’s working on one OS, doesn’t mean it’s going to work on another!