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 [import]uid: 26027 topic_id: 11155 reply_id: 311155[/import]

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:

[code]

math.randomseed( os.time() )

[/code] [import]uid: 5833 topic_id: 11155 reply_id: 40493[/import]

great, thanks for the insight [import]uid: 26027 topic_id: 11155 reply_id: 40498[/import]

No worries, hope it helps. [import]uid: 5833 topic_id: 11155 reply_id: 40503[/import]