Civ3's random number generator

Discussion in 'Civ3 - General Discussions' started by Plume, Dec 9, 2002.

  1. Plume

    Plume Chieftain

    Joined:
    Mar 4, 2002
    Messages:
    35
    I just read something about civ3 RNG (random number generator) in another thread and I think RNG's need a bit of defending. At the same time, I'll share some of my opinions about the RNG with you. First, here's how a RNG's work (briefly, I'm not an expert).

    Basically, it starts with a seed. That's a number generated (usually) by the internal clock of the computer. By using some sort of combination of the date and time (up to the millisecond), you get a number. Then, that number is fed into a function. This is the mysterious part. Essentially, this function is "chaotic". That means that it's very complicated and that, more importantly, if you start with a seed of say 0.5123, you'll get a very different number than if you had used 0.5124. The other important characteristic of this function is that you'll get something "random". I'll explain what this means below. To get the second "random" number, you feed the first one into the function and bingo, you've got what is essentially a random sequence of numbers. In Civ3, this new number is kept (if you keep the flag "keep seed" on) in the savegame. Next time you load a game, you're at the same point in the sequence.

    The random in " " means basically that any sequence you'll get using this method will (using mathematical analysis) satisfy very precise mathematical conditions that a purely "theoretical" random variable would also satisfy. However, the sequence is not completely random, eventually, you can get back to your starting point. In fact, one of the properties of an RNG is how many iterations of the function will bring you back to your starting point, on average. The bigger the number, the better the RNG is. But we're talking about huge numbers here, in no way detectable by someone over a couple of tries.

    My description might not be entirely right (different RNG works slightly differently) but there's no list involved and certainly no human intervention. In fact, it's been proved that if you take two sequences of numbers, say the first one was created by a human and the second using a RNG, then it's quite easy to see that mathematically, the RNG will give you a sequence that could be random but the human will always have patterns.

    I'm no expert here either but one of the major flaw of humans when generating random numbers is that we tend to "even things out". For example, let's take "1,1,1,1,1" and "2,6,4,9,3". Observation one: "1,1,1,1,1" seems a lot less random than "2,6,4,9,3" right? Well, right AND wrong. Ok, I'll try to make this short but bear with me.

    Right: It IS true that "1,1,1,1,1" is less random than "2,6,4,9,3". Why? Human beings are the product of evolution in nature. Nature is not random. If you live in ancient times and notice some sort of pattern in the stars, it can help you find directions, tell what time of year it is and so on. Basically, humans are good at recognizing patterns (that's one of the basic elements of intelligence). And nature is full of patterns, waiting for some smart human to find them. That's what chemistry, astronomy, physics, biology, statistics are all about. In the above sequence, any sane person would tell me that the next number is going to be 1 in the first sequence and well, unknown in the second. It comes down to this, if I drop a ball, will it fall down or up? Well, down. But why? Because every time I've dropped a ball in my life, the ball has gone down and there's no reason for me to think that it will be different this time.

    BUT, the thing is, mathematics and computers are not like nature. That's why so many people struggle with math, sometimes it's intuitive but often it's not. The fact of the matter is, in all possible sequences of five numbers between 1 and 10, they both have the same chance of occuring. Don't get me wrong, there's is (a lot) of randomness in nature. It's just that human beings (have evolved and) are trained at detecting patterns and sometimes, we see patterns that aren't there (e.g. all those people who think they've discovered a new fantastic secret about how you can beat the odds of combat in civ3 by doing this and that).

    Now I said something above about "evening things out". This is something everyone does. If you play at a slot machine and lose ten times, do you have a better chance of winning the 11th time? If you attack a spearman with a knight a lose 4hp in a row, do you have a better chance of winning with the next knight? I'm sure most people will say no and they're right. That's because mathematically, the numbers in the sequence are called independant (another big property of RNG's and true random things, like dice). It doesn't matter what happens with the first knight, the second knight will attack using independant numbers.

    But the fact of the matter is (this is where my opinion starts...), it seems wrong. In fact, I would go so far as saying that the RNG in civ3 works too well. It's too random. And quite frankly, that's not how things work out in real life. The RNG needs to even things out. Now if you've followed me so far, please read until the end. I'm not against a tank losing to a spearman (it has never happened to me anyway). What bugs me is this. When the RNG gives you numbers, the law of large numbers (or the other one can't remember) tells us that after N of these numbers, assuming it's uniform discrete between 1-10, we'll get approximately N/10 ones, N/10 twos and so on. The problem is that N generally has to be big. In other words, "1,1,1,1,1" could happen but eventually, things will even out and you'll start getting 2's and 3's and the others. My point is this: random sequences have streaks in them (like "1,1,1,1,1") but the RNG in a game made for people should be built to minimize the length of those streaks. In other words, the number generator shouldn't be random. Or put another way, there should be an integrated system which tells the RNG "hey man, you've given me 1,1,1, now give me something bigger!".

    Now how would this affect gameplay? Well, basically, it would mean that if you're very unlucky (losing a knight to warrior in open terrain) then the next knight would have (slightly) better odds than a completely random fight. Of course, you can't go overboard with this idea. Otherwise, you'd get in a situation where if you're really unlucky for a while, then you use a knight to attack a rifleman and hope to win. But there should be a little bit of short term compensation for being very unlucky (or lucky!). I think it would make the game more realistic. What do you think? In any case, thanks for reading.
     
  2. Grey Fox

    Grey Fox Master of Points

    Joined:
    Dec 19, 2001
    Messages:
    8,726
    Gender:
    Male
    Location:
    Sweden
    I both agree, and disagree.

    The RNG will compensate eventually, as there are only 2 outcomes anyway. Loss and win.

    In the big run, you will have won about 50% (if all fights were equal), maybe a little less, maybe a little more.
     
  3. Ronald

    Ronald Emperor

    Joined:
    Nov 5, 2001
    Messages:
    1,009
    Location:
    Vienna, Austria
    As Grey Fox stated: In the long run it will even out. (Altough some famous economist - I can't remenber the name - also stated in the long run we all will be dead :) )

    So, there will be lucky games and unlucky ones because in the early beginning it really depends most.

    If you are trying to defeat a spearman with 6 archers (your entire army at this point) and all die will have much bigger impact on your game as when 10 out of your 50 cavalry die to defeat a rifleman.
     
  4. etj4Eagle

    etj4Eagle ACME Salesman

    Joined:
    Dec 6, 2001
    Messages:
    614
    Location:
    Columbus, OH
    Actually the long strings of improbable results that the random number give are seen in real life. As a simple test take a coin and flip it a hundred times and then look at the patterns of heads and tails. There are tests to detect fudging of data / audit numbers by looking for these "improbable strings."

    And as you did agree the RNG in Civ3 works fine. There is an old thread where one poster actually hacked into the game to access the RNG and run it through a statistical test suite and it passed.
     
  5. sumthinelse

    sumthinelse civ investigator

    Joined:
    Mar 20, 2002
    Messages:
    1,333
    Location:
    Austin Texas USA
    Plume, you have not been the first to suggest this. Lt. Killer M. (and probably others) said essentially the same.

    Let's say that if I lose 3 combats in a row you get a higher probability of winning the next time. If I attack a rifleman fortified in a city with a stack of warriors, then after 3 losses, my chance of winning starts increasing. If you are not careful with the way you change combat, you will have exploits for the attacker.

    I keep thinking about Stalingrad in world war 2. How did the Russians hold out? One way to look at outcomes where you lose more attackers than you expect is that the RNG can make a "rugged defense" possible.
     
  6. Lt. 'Killer' M.

    Lt. 'Killer' M. Deity

    Joined:
    Dec 5, 2001
    Messages:
    7,475
    sumthinelse: (slightly OT) am doing some tests on this, especially since the jungle 25% def bonus seems to be faulty in PTW - would you help me with the statistics?????


    preliminary findings: a 'good' average often needs over 50 battles - meaning the smaller your army (and the more important each unit and battle) the more it is luck, not planning. Actually up to a point where it seems to that as long as you have an early war it doesn't really matter if you 'can do' the other aspects of the game - luck can safe any bad start and destroy any good one.
     
  7. anarres

    anarres anarchist revolutionary

    Joined:
    Apr 22, 2002
    Messages:
    6,069
    Location:
    www.civ3duelzone.com
    The whole point of the RNG is that it is random. I would not be amused if they changed the randomness of the RNG.

    In the long run you will win 50% of the 'even' battles. If you change the RNG in the way you suggest you would still win 50% of the 'even' battles as it would have to stop your winning streaks as well as your losing ones.

    Runs of bad/good 'luck' are a fact of life. (In fact, they are a fact of randomness, a fine mathmatical concept, and as such 'luck' is the wrong word). In reality, you do get very bizarre coincedences that 'appear' random. This is inevitable when you have a large enough sample size.

    For example, occasionally you get 'cancer clusters' in geographical areas. These may seem strange, but in a large enough area, you will always get pockets where there are many more cases than the average. Conversely, you will also get areas with very few cases. In either isolated instance it would appear abnormal, but mathmatically, it is very likely to happen somewhere. What would be more worrying is if nowhere had high or low readings. This is statistically very unlikely to happen.

    As a mathmatician, I shudder to think of any other way to resolve battles where you want a 'random' element.
     
  8. Four Four Seven

    Four Four Seven Spearman

    Joined:
    Feb 20, 2002
    Messages:
    83
    Location:
    Massachusetts, USA
    I was with you for a while. (I didn't agree with all of your comments - such as "Mathematics is not like nature" when mathematics is a remarkably good model of how nature acts. But I understood your points). But I lost it on the italicized point - How would less randomness make it more realistic?

    As you know, if you flip a coin, and get heads ten times in a row, you will not have a greater chance of winning the eleventh time.

    If, in real life, I send an army to battle with a 50% chance of winning, and it loses (assume "losing" means all armies don't return and there's no knowledge gained), and I send another army to fight the same foe (again, assume the other army gains no knowledge), they will again have a 50% chance of winning. It does not get higher just because of a loss by an independent army.

    Another interesting bit of info many people don't get - let's say I flip a coin many times. The more I flip it, the closer to 50% heads and 50% tails. However, the more I flip it, the greater the expected difference between actual numbers of heads and tails. i.e. - flip a coin 10 times - the difference between heads and tails can't be greater than ten, and is unlikely to be greater than four or six. But if you flip a coin 1000 times, although you will be closer to 50% for heads and tails, you could easily end up with, say, 530 heads and 470 tails, a difference of 60.
     
  9. TheNiceOne

    TheNiceOne Emperor

    Joined:
    Feb 6, 2002
    Messages:
    1,372
    Location:
    Norway
    Four Four Seven: A very good post, especially the last point which I think is a commom misconception.

    Personally I would not want to see any "memory" in the combat system which made one attack result dependant on the previous (possibly in another front) - as it would simply feel wrong to even have a system which opens up explots as this: Attack first on an unimportant front, and then turn to the important attacks as soon as you've had some bad luck. The important attack will now have a better chance of success.

    One place where such a RNG with memory would have a place (IMO) is in creation of leaders. A 1/16 probability on something which is so extremely important means that getting a leader on the 1st elite win or on the 50th (which both happens now and then) can make or break a game. Gaining leaders with a bit more regularity (but still 1 in 16 elite wins) would improve the game IMHO.

    But I think the CIV3 combat system is mostly good. I would like to see the better unit win a bit more often than now during the ancient age only, since the first wars contains very few units, but I don't think wars after the ancient age should be less random than now. It's hard to fix this in the editor though...
     
  10. anarres

    anarres anarchist revolutionary

    Joined:
    Apr 22, 2002
    Messages:
    6,069
    Location:
    www.civ3duelzone.com
    This ties in with what I was saying (and I nearly used coin flips as an example). You would expect in 1000 flips to get somewhere 'h,h,h,h,h,h,h'. In fact the chance of this in any 7 flips is only 1 in 128 (or 1 in 64 if you accept 't,t,t,t,t,t,t' as well). If you have that many fights in civ3, you may well get 7 good 'flips' in a row, or indeed 7 bad ones. If you played 1000 battles and didn't get 7 good and 7 bad in a row then that would be unusual.
     
  11. sumthinelse

    sumthinelse civ investigator

    Joined:
    Mar 20, 2002
    Messages:
    1,333
    Location:
    Austin Texas USA
    Let's take an example. I attack a regular fortified spearman in a city with a stack of 3 regular swordsmen. I expect to lose all 3 swordsmen about 1/5 of the time. So if I attack 5 cities defended by a spearman, with 3 swordsmen each time I should lose all the swordsmen 1 time (on average). But if I turn off the "preserve random seed" and repeat the attack on 5 cities several times, I should:

    Win all 5 cities: 30% of the time

    Win 4 or 5 cities: 71% of the time

    Lose 2 or more times: 29% of the time

    Lose 3 or more times: 7% of the time.

    So with a total of 25 attackers, you get what would be a disastrous result early in the game , losing at least 15, 7% of the time.

    Is that what you mean?
     
  12. anarres

    anarres anarchist revolutionary

    Joined:
    Apr 22, 2002
    Messages:
    6,069
    Location:
    www.civ3duelzone.com
    Just remember that 7% is less than 1 in 14. That's pretty unlikely.
     
  13. sumthinelse

    sumthinelse civ investigator

    Joined:
    Mar 20, 2002
    Messages:
    1,333
    Location:
    Austin Texas USA
    Tell me what units you are using, hit points of each, how many in each stack, and of course whether they are fortified. The less probable the event, the more trials you need to get a "statistically trustworthy" test result.

    I find this calculator to be very useful:

    http://fonsg3.let.uva.nl/Service/Statistics/Binomial_distribution.html
     
  14. anarres

    anarres anarchist revolutionary

    Joined:
    Apr 22, 2002
    Messages:
    6,069
    Location:
    www.civ3duelzone.com
    @sumthinelse:
    spearman fortified in a city (on grassland/tundra/desert) is defense 4.2?
    = 2 +50%city +50%fortified +10%terrain = 2 * 2.1 = 4.2

    Swordsmen are attack 3, so what are the odds of winning? Unless we know the exact formula used, only 'even' battles should be considered.

    For the probability to be 1/5 (for 3 losses in a row), the probability of each loss (call it x) is:
    x cubed = 1/5,
    so x = 0.2 to the power 1/3 = 0.585

    If x=1/2 (for an even battle), the odds of winning are 1/8 (1/2 cubed).

    Please explain your numbers as I don't understand.
     
  15. Yndy

    Yndy Emperor

    Joined:
    Jun 29, 2002
    Messages:
    1,415
    Location:
    Romania
    I understand Plume's point but i believe that the RNG delivers that just by being RNG. Long streaks of wins or losses are there just to demonstrate that the results are random. But as the number of consecutive losses increase the probability that the next event is a win increases EVEN IF there is a 50/50 chance individually of that event to occur.

    In normal talk if you lose a tank to a spear there is a huge probability that the next tank will win.

    Or, If you would like to see that from another angle: say you won 1,000,000 bucks at the lottery. Do you really expect to win another 1,000,000 bucks at lottery? No and you should never bet again in your whole life (it's a once in a lifetime event). But statistically you have the same chance of winning the next time you buy a ticket.

    About what 447 said about testing a 50-50 event 1000 times. Chances are that the difference would not be 60 as you said but much lower.
     
  16. Grey Fox

    Grey Fox Master of Points

    Joined:
    Dec 19, 2001
    Messages:
    8,726
    Gender:
    Male
    Location:
    Sweden
    NO, the spearmans defense in that situation is 3.7

    2 + 50% city + 25% Fortification + 10% Grassland =

    2 + 1 + 0.5 + 0.2 = 3.7


    EDIT: This one works real good: http://www.civfanatics.com/civ3combatcalc.html
     
  17. Physicist

    Physicist The Universe is flat!

    Joined:
    Nov 19, 2002
    Messages:
    205
    Location:
    Germany
    Some very good posts here. I would like to add some comments:

    @Plume: You said that "mathematics and computers are not like nature" and that natural sciences "are all about [patterns and the human finding them]". I know what you mean but I have to disagree. I personally can tell you about physics: Understanding physical processes often means understanding the interactions of microscopic particles. These particles follow certain patterns, but they do that in a random manner.

    For example, imagine a radioactive nucleus (as substitution for your Civ unit fighting a combat). From physical laws (=pattern) I can tell you at any time the chance that it decays into another nucleus (= unit looses) and the chance that it does not decay (= unit wins). However, I am unable to tell you whether it really will decay at this given moment or not. However, given not only one nucleus but a large amount of nuclei the "chance of decay" transforms into a distribution of decaying (sp?) and not decaying nuclei (with a deviation that depends on the number of nuclei as described in other posts). The same is true for the movement of particles etc. Actually, the determinstic (i.e. without random element) physics laws we learn in (the first years of) school are only true as large bodies, consisting of many particles, are involved. Basically, the particles in these large bodies follow the laws of quantum mechanics in a random manner. But due to the huge amount of particles involved the result becomes deterministic (and the deviation mentioned above still exists but is too small to be measured). That's why you basically can exactly determine what happens when a two balls hit on a billard table, but not when two electrons hit.

    Of course the way to create random numbers in a computer (that does not know random numbers/effects per se) is something that can be discussed. I am not an expert in that but I can tell you that the way you described it in your post is exactly the way it is usually done in physical simulations ("Monte Carlo simulations" for those who are interested). You mentioned a completely correct and very important point when you said that the RNG actually runs through a list of (hopefully) random numbers and might eventually return to its starting point after some time. That is the point where RNG results really are not random any more, and this is exactly what should not happen simulations or games :) . Thus, before starting a simulation, you have to test the RNG. In our case, I suppose/hope that Firaxis created a list long enough to avoid repeating numbers, but I do not know.

    As conclusion: Using random numbers *is* the one and only way to determine the combat results etc in Civ. It is exactly the way nature works, and on the long run "bad luck" *will* be compensated by "luck". An easy way to reduce the "randomness" of combat results is to give the units more hit points, as some people (e.g. Lt. 'Killer' M. (I think)) already do in their mods or as it was done in Civ2. More hit points means less deviation of the observed distribution of combat results from the theoretical chance to win, thus less "spearmean kills tank" events.

    @Yndy:
    Quote:
    "say you won 1,000,000 bucks at the lottery. Do you really expect to win another 1,000,000 bucks at lottery?"

    Actually, do you expect to win $ 1,000,000 at all when buying one ticket? No. (But you can hope :) )

    Thank you for reading.

    Edit: Typos
     
  18. sumthinelse

    sumthinelse civ investigator

    Joined:
    Mar 20, 2002
    Messages:
    1,333
    Location:
    Austin Texas USA
    Fortified is +25%, not +50%. "Fortified", not "Fortress".

    The civ3 combat calculator

    http://www.civfanatics.com/civ3combatcalc.html

    says odds of the swordsman losing a single combat are .597. This assumes swordsman has 3 hit points and so does the spearman, so whoever loses 3 hit points is dead.

    .597 cubed is about .21, so the stack of 3 swordsmen should take the city about 4/5 of the time.
     
  19. sumthinelse

    sumthinelse civ investigator

    Joined:
    Mar 20, 2002
    Messages:
    1,333
    Location:
    Austin Texas USA
    Oops, Grey fox beat me to it. :)
     
  20. anarres

    anarres anarchist revolutionary

    Joined:
    Apr 22, 2002
    Messages:
    6,069
    Location:
    www.civ3duelzone.com
    Yndy, you have fallen in to the trap most people make when dealing with probability. Knowing the past with this kind of distribution (binomial) is of no use in predicting the future.

    The odds in all of these cases is unaffected by the previous results. If you lose a tank to a spearman, you are no more or less likely to win or lose the next time. If you roll a fair die 100 times and get 100 6's in a row, the odds of the next one being a 6 is still 1/6.

    Believe me, discrete maths is my speciality :)
     

Share This Page