Random Numbers: math.random() & math.randomseed()

Hi,

My game relies on selecting random images from an images array (with 100 images).

Every time I run the game I get the same sequence of 'random images'. I am using math.random(1,100) to select the image from the image array:
images[math.random(1,100)]

I need a random sequence each time the game plays. How can I be sure of this?

I know there's strictly no such thing as a 'random' number in Computer Science, but the fact the math.random generates the exact same sequence each time I find quite surprising.

Is my answer to use math.randomseed() or some combination thereof?

Please can someone share their experiences?

Thank you,
Paul

You are correct that you need to seed the generator. You can either seed it with a specific number to ensure you always get a known system, this can be great for testing however not that great in a real situation. What you need to do is seed it with a different number each time and the easiest way to do that is to use the current timestamp, simply place the following code at the top of your main.lua file:

1
math.randomseed( os.time() )

great, thanks for the insight

No worries, hope it helps.

views:1439 update:2011/9/29 19:21:19
corona forums © 2003-2011