2013/06/28

Randomness in games part II

Guess what: random numbers on computers aren't random. They are generated by algorithms and math tells us that formulas are not random. Proper random number generators also take CPU time - which is negligible unless you need thousands of random numbers per second - as it is in most online games (with combat drawing most random numbers).

The problem with programming is that lazy ones simply use the supplied rand() function supplied by their compiler. But compiler writers rarely spend a lot of time creating great random number generators.

What does random mean anyway. From our viewpoint it means that I get various results for a given action. I read feedback by the game and see some randomness to create variances in the game.

Imagine I would lose due to random numbers 3 times in a row. "That sucks" is the first reaction. But can it be? Sure it can, a random number generator theoretically could create numbers which lets you lose 10 times in a row. Not great, and bad for the game - so we try to prevent it.

Yes, I will now explain how we can make sure this doesn't happen, basically making random less random.
We need to make sure that there is no win or loss streak in the numbers, so we need to distribute them evenly. We also need to make sure that its random, not sorted.

We simply take a table (let's say 10.000 entries) and we fill the table with numbers from 0-99, sequentially until the table is full. Now we start to randomly exchange numbers until the table is totally jumbled and "random". Still streaks can appear so we analyze the table for those and remove them, we make sure there is a rough even distribution of random numbers for every sample we take.

Sounds confusing? Check these links out for some table references:

http://en.wikipedia.org/wiki/A_Million_Random_Digits_with_100,000_Normal_Deviates
http://www.rand.org/pubs/monograph_reports/MR1418.html

So these tables guarantee that I get true random numbers but no winning streaks or losing streaks - AND they are much faster for the game as I simply take one number after the other.
AND they can be persistent as I simply save the index (the current random number I took) for each player - so the player doesn't see a repetitive behavior in the game.
So now you know how World of Warcraft generates their randomness. As I knew how they work I could even play with their randomness and increase the likelihood of epic drops. See my 7 year old blog entry which explains this:
http://teut.blogspot.de/2006/11/my-t1-item-xyz-doesnt-drop.html

Important: Never use these tables for gambling sites like Poker, otherwise some bright MIT students will forecast your 'random' numbers and loot your savings! (it really happened to Poker sites years ago).

So now you know how to generate the numbers - but how do we use randomness? If a game is truly random its actually very boring as random feedback doesn't create a good game.

But this is part of on of my next blog entries - coming soon.