math.random() not producing random numbers

I noticed while testing my game earlier that math.random() was not producing random numbers on my iPod Touch 2g (running iOS 4.2.1).

It almost was, but it was the same random numbers every time, almost as if I had seeded it, but I hadn't. It worked fine on the simulator and on my Droid Incredible. I finally "fixed it" by actually seeding it with the current time, like so:

1
math.randomseed( os.time() )

That is the way math.random will function in most languages. Always seed the generator if you want truly random values.

I think this generators never gives you "truly" random numbers. Seeding makes no difference.

The point is, you can produce randomly looking sequences of numbers, and you can reproduce sequences by the same seed what can be useful - in this case "not truly" is useful.

I think the generators of different programming languages/systems use the unseeded random() different. Some seed with an more or less truly random value - maybe some are seeded by zero or another fix value.

In the most practically sense you can use the well seeded sequences as "truly" random - because the sequence is not or hardly predictable.

Always seed with a good value to be not predictable ;)

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!

Hmm... I assumed that math.random() without a specific seed value would use the system time as the seed.

Oh well, seeding it isn't a big deal, but the documentation should probably be updated to reflect that math.random() without a seed doesn't work.

Dave

views:2221 update:2011/10/19 14:58:09
corona forums © 2003-2011