math.random not random on device?

I use the following 6 times in my code to generate different numbers to load 6 random images:
local r = math.random ( 1, 5 )

… which works find in the simulator, but when I try it on the iPad it gives me the same numbers each time.

Is this normal? Is math.random() not entirely random on the device. Is there another method I should be using to choose a random number?

Many thanks!

t. [import]uid: 90222 topic_id: 15305 reply_id: 315305[/import]

Never mind, it’s apparently a “feature” of Lua. Possible solution here:
http://hellomobiledevworld.blogspot.com/2011/08/using-mathrandom-in-corona-sdk.html [import]uid: 90222 topic_id: 15305 reply_id: 56500[/import]

The requirement of a seed value is not specific to Lua, it is how all random number generators work. By not supplying a seed value you will be guaranteed to always get the same values each run through, i.e. if you called it three times at start up you may get 1, 6, 20 and then if you started the app again you would get that same sequence. This is great for things like unit testing.

The other issue about the need to call it a few times does indeed sound like a bug though which I have read about a couple of times. [import]uid: 5833 topic_id: 15305 reply_id: 56508[/import]

As I suggested in my Blog entry, it is a feature and not a bug.
In some languages (like .net) the default seed is the system’s time. It is makes the life of the developers easier, as most of them using random without knowing anything about seed (for the good and the bad).

This specific blog entry is very popular - so I guess too many new developers (and me included)cannot making it work in the first try :slight_smile:

Roni. [import]uid: 26945 topic_id: 15305 reply_id: 56741[/import]

Hey Roni. Thanks for writing the post! :slight_smile: [import]uid: 90222 topic_id: 15305 reply_id: 56745[/import]

I found this helpful, thanks for the link.

Do you know if it’s possible to have more than one function using math.random, does each math.random produce it’s own separate value.

like say for example

object1.x = math.random(0, 100)

than few lines later

object2.x = math.random(0, 100)

by seeding the system I’m assuming both objects now have different x values?

I found this helpful, thanks for the link.

Do you know if it’s possible to have more than one function using math.random, does each math.random produce it’s own separate value.

like say for example

object1.x = math.random(0, 100)

than few lines later

object2.x = math.random(0, 100)

by seeding the system I’m assuming both objects now have different x values?