Procedural map generation running on device results in the same map sequence.

I have been working on my first project in Corona, it is a procedural cave generator that has several nodes that crawl around randomly to build a cave.

When testing in Corona SDK it works great, every time I load a new map or restart the program it shows a brand new random map.

However… Once I build my system for IOS and put it on my iphone the maps come up in a sequence that is nearly 100% predictable. The maps seem to be cached into memory or something.

I have tried restarting the app, cold booting the phone, and republishing the app then reinstalling it.

But every time I start the app it starts with the same map layout, then the next screen is a different map than the first screen, but the second map is always the same map… and the third and the fourth and so on.

So the maps ARE generated randomly, but it seems like the random numbers are a defined sequence on the iphone which is resulting in a set design for the maps in my game.

Is this a bug? Is there any way to make random numbers differently? Because it works 100% of the time everywhere except on the actual device. [import]uid: 74542 topic_id: 12950 reply_id: 312950[/import]

have you tried using

math.randomseed( os.time() )

? [import]uid: 43961 topic_id: 12950 reply_id: 47509[/import]

I also saw something about throwing away the first number returned too.

But I agree, you may need to seed the random number generator. [import]uid: 19626 topic_id: 12950 reply_id: 47517[/import]

Thanks! I’ve never come across that before.

Here is a link that I found that explains seeding.
http://lua-users.org/wiki/MathLibraryTutorial

But I can’t seem to run the seed multiple times. Can you only seed a random number once?
[import]uid: 74542 topic_id: 12950 reply_id: 47521[/import]

you should be able to run it multiple times why do you think you cannot? [import]uid: 43961 topic_id: 12950 reply_id: 47522[/import]

Not sure why you’d need to seed random more than once after launching. Assuming seeding with os.time actually works calling it once should result in a different sequence every launch.

But according to a comment in the link you provided, seeding with os.time is not necessarily going to result in a different sequence. They suggested doing something like this for more reliable randomness:

-- improve seeding on these platforms by throwing away the high part of time, -- then reversing the digits so the least significant part makes the biggest change -- NOTE this should not be considered a replacement for using a stronger random function -- ~ferrix math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) ) [import]uid: 9422 topic_id: 12950 reply_id: 47539[/import]

There are reasons to reseed, but those are usually things like card games where you want to play the same game again. Simply storing the seed for each game, and reseeding the random number generator with the game ID will give you a repeatable list of numbers so your cards come up in the same order.

I was scoping out a MMO based in space and to store all the information for a solar system and all its planets was huge, but store a random number seed and then you can regenerate the entire system off of one 32 bit number.

But in 99% of the cases, you should only need to seed it once with as random of a variable as you can get from the environment. Time is a good value, but time * pid is better. The technique listed above is quite good too.
[import]uid: 19626 topic_id: 12950 reply_id: 47547[/import]