ruff_hi
Live 4ever! Or die trying
There has been talk about the Civ4 Combat Odds and RNG
http://forums.civfanatics.com/showthread.php?t=336088
However, the whole discussion is based on the assumption that the random number generator is 'good'. As mentioned in the thread above, any series of numbers is as random as any others. The following coin toss sequences each have exactly the same probability of occurrance ...
Over time, there is the expectation that the number of heads and the number of tails will balance out. There is no single statistical test that we can use to determine if a series of numbers is random or not. However, there are ways of judging if a series of numbers appears to be non-random. Lets see how Civ4's random number generator (RNG) does.
I used the following code (in BUG's logger) to generate 5000 pairs of random numbers ...
Then I dumped the resulting numbers into excel and generated some graphs.
Test 1 - is the observed average close to the expected average?
I calculated a moving average over 250 numbers. The expectation is that the average will be 500. Here is the result ...
You'll note that the average is quite lumpy but it does bounce around the 500 mark ... sometimes a little low, sometimes a little high. If anything, the blue line is probably on the low side more than the high side. Looking over 250 random numbers for an average is probably not sufficient (too much noise / randomness).
An Aside About Random Number Generators
Computers are good at generating lots and lots of numbers. Give them a formula, plug in the inputs and you get the output. Change the input and you change the output. Put in the same inputs and you get the same output. So ... how are random number sequences generated? Well, there are a number of methods that I will not cover here, but the interested person can check wikipedia (http://en.wikipedia.org/wiki/Random_number_generation). A summary of the computational method is that the random sequence can be thought of as a very very long chain of numbers that loops around to the starting position. You provide a seed to the RNG - a starting link in the chain - and then go from there, 1 chain at a time until you loop back to your starting position. Most RNGs have very long return periods. Ideally, each link the chain will be a number such that the series of numbers appears random.
Right - enough of the lesson, back to the graphs. Here is the same graph as above, but using a moving average of 750 numbers ...
You'll notice that the lines are much smoother, closer to 500 but still are not exactly equal to 500 all the time. Again, the blue line looks a little low.
Examining these two graphs, there is nothing to say that the series of 500 pairs do not appear non random. I would have to say that Civ4's RNG passes Test 1.
Test 2 - Is there a pattern between 1 number and the next?
The way to test this is to create a scatter plot of one number v the next number. If there is a relationship, then you will see clumping in the scatter plot.
Note: that this is a 1000 v 1000 plot (and the x axis is longer than the y axis - stupid excel) that only has 5,000 plots (out of 1,000,000). Based on this data, I cannot see any pattern between one number and the next.
Test 3 - Runs of numbers
I counted the number of sub-500 numbers (or over-500 numbers) in a row. Effectively, this is the same as counting Head and Tail runs in a coin toss. If I toss a H, then I have a 50/50 chance that the next toss is a T. This means that I will have a 'run' of 1 head. So, the probability of getting a run of 1 H is 50%. The probability of me getting a run of 2 Hs or 2 Ts is 50% x 50% or 25%. 3 in a row is 50% ^ 3, etc.
You certainly cannot say that the Civ4 RNG fails this test.
http://forums.civfanatics.com/showthread.php?t=336088
However, the whole discussion is based on the assumption that the random number generator is 'good'. As mentioned in the thread above, any series of numbers is as random as any others. The following coin toss sequences each have exactly the same probability of occurrance ...
- HHTTHTTH
- TTHHTHTT
- HHTTHHHH
- THTTHTHT
Over time, there is the expectation that the number of heads and the number of tails will balance out. There is no single statistical test that we can use to determine if a series of numbers is random or not. However, there are ways of judging if a series of numbers appears to be non-random. Lets see how Civ4's random number generator (RNG) does.
I used the following code (in BUG's logger) to generate 5000 pairs of random numbers ...
Code:
for i in range(1,5001):
i1 = gc.getASyncRand().get(1000, "")
i2 = gc.getASyncRand().get(1000, "")
message = "%i, %i, %i" % (i, i1, i2)
Logger.writeLog(message)
Test 1 - is the observed average close to the expected average?
I calculated a moving average over 250 numbers. The expectation is that the average will be 500. Here is the result ...
You'll note that the average is quite lumpy but it does bounce around the 500 mark ... sometimes a little low, sometimes a little high. If anything, the blue line is probably on the low side more than the high side. Looking over 250 random numbers for an average is probably not sufficient (too much noise / randomness).
An Aside About Random Number Generators
Computers are good at generating lots and lots of numbers. Give them a formula, plug in the inputs and you get the output. Change the input and you change the output. Put in the same inputs and you get the same output. So ... how are random number sequences generated? Well, there are a number of methods that I will not cover here, but the interested person can check wikipedia (http://en.wikipedia.org/wiki/Random_number_generation). A summary of the computational method is that the random sequence can be thought of as a very very long chain of numbers that loops around to the starting position. You provide a seed to the RNG - a starting link in the chain - and then go from there, 1 chain at a time until you loop back to your starting position. Most RNGs have very long return periods. Ideally, each link the chain will be a number such that the series of numbers appears random.
Right - enough of the lesson, back to the graphs. Here is the same graph as above, but using a moving average of 750 numbers ...
You'll notice that the lines are much smoother, closer to 500 but still are not exactly equal to 500 all the time. Again, the blue line looks a little low.
Examining these two graphs, there is nothing to say that the series of 500 pairs do not appear non random. I would have to say that Civ4's RNG passes Test 1.
Test 2 - Is there a pattern between 1 number and the next?
The way to test this is to create a scatter plot of one number v the next number. If there is a relationship, then you will see clumping in the scatter plot.
Note: that this is a 1000 v 1000 plot (and the x axis is longer than the y axis - stupid excel) that only has 5,000 plots (out of 1,000,000). Based on this data, I cannot see any pattern between one number and the next.
Test 3 - Runs of numbers
I counted the number of sub-500 numbers (or over-500 numbers) in a row. Effectively, this is the same as counting Head and Tail runs in a coin toss. If I toss a H, then I have a 50/50 chance that the next toss is a T. This means that I will have a 'run' of 1 head. So, the probability of getting a run of 1 H is 50%. The probability of me getting a run of 2 Hs or 2 Ts is 50% x 50% or 25%. 3 in a row is 50% ^ 3, etc.
You certainly cannot say that the Civ4 RNG fails this test.