Unless you have some very sophisticated hardware, no random number generator is going to be truely random.
Every common computer from an IBM PC to a Mac to an iPhone uses something called a Pseudo random number generator. This random number generator produces random sequences of numbers based on a starting seed.
The more different that seed is from run-to-run the more random the numbers will appear. If you seed the random number generator with the same seed, you will always get the exact same pattern of numbers.
Where this becomes a point of frustration is that the Corona SDK Simulator appears to not initialize memory to 0 and math.random’s unseeded value will be whatever value was last left in that memory, giving the simulator the feeling that its generating random numbers with out you specifically seeding it.
But when you go to a device, you’re pretty much getting a zero’ed memory block for your app, and the random number generator is being seeded with 0 all the time and you suddenly don’t get random numbers any more.
A best programming practice is to always seed the random number generator at the start of your program. This has been true for 50+ years and it will be true for the next 50.
Using the system time in seconds is a reasonable value, but there are other better options, like taking the number of seconds and reversing the bits so the least significant digits become the most significant, or multiplying the time by the process ID. Unfortunately in Lua its difficult to manipulate bits and we don’t have API calls to get the PID of your app so os.time() is the easiest choice to go with.
Moral of the story: ALWAYS SEED! [import]uid: 19626 topic_id: 16408 reply_id: 61287[/import]