I was just testing different ways for shuffling data in an array and found that initializing the random generator from os.time() is not my cup of tea because I am pretty impatient and can't wait a second before restarting :)
Besides that random numbers by seeding with os.time() may lead to predictable sequences for cheaters! As they can seed a computer program with the same time value easily and let calculate the resulting sequence in advance to the game. Which is of course not valid for every application of the numbers generator.
So I looked at the opportunities for randomizing more random and even not predictable!
I came to the following conclusion:
1 2 3 4 5 | -- Seed the Random Generator more Random require('socket') local n,f=math.modf(socket.gettime()) math.randomseed(f*1000000) math.random() -- was in forum to overcome a problem with repeating first values |
To validate this on the simulator and the device I used the following code for testing
1 2 3 4 5 | local textObject1 = display.newText(f*1000000, 50, 50, nil, 24 ) textObject1:setTextColor( 255,255,255 ) local textObject2 = display.newText(math.random(1000), 50, 100, nil, 24 ) textObject2:setTextColor( 255,255,255 ) |
I use the socket.gettime() for getting random values which are not predictable with the current implementations I tested on the simulator and the devices I own.
I also added the "one" empty call which is mentioned here in the forum. I do not know if it is really needed. At least it will not hurt :)
For the paranoid: One may setup a timer event to re-seed every some (random) seconds by using a similar function to avoid identification of the current location in the pseudo random number generators sequence :)