Announcement

Collapse
No announcement yet.

Random number generator: How does it work?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random number generator: How does it work?

    How exactly does it work? If you're going to get a leader, you will if you happen to repeat the same sequence of events ten times.

    Sometimes, though, you get a really absurd result like a Spearman killing a Tank, so you just can't help but load the game out of frustration. You only get out of the crazy event if you execute another combat, save, then redo the supposed cakewalk attack.

    Same thing seems to happen with defections, etc.

  • #2
    There is something called the "seed" value. When the computer has to generate a random number, it takes the seed value and generates the random number by applying some arthmetical and/or logical opration to the seed value. Most often the seed is taken from the clock of the computer, therefore every time you run the same sequence, you'll get different numbers. On civ3 however, it seems like the seed value is stored to the save file, so when you load the game and do the same sequence of actions, you'll get the same sequence of results.

    Comment


    • #3
      There has already been a lot of discussion about this. Long story short, the random number seed is saved in the save file so that when you reload and follow the same sequence of events you'll get the same result. This is designed, apparently, to prevent save/reload cheating. The reason the results change if you fight the battles in a different order, for example, is that a different number of random numbers is used, thus changing subsequent battles.

      If you want more details, see this
      thread and this thread

      Comment


      • #4
        Hey, thanks, I didn't know about them. But that was a bit hard to go through... what's the simple answer to what actions change the random combat seed number?

        It gets annoying sometimes when the fixed number leads to outrageous results, like getting your elite cavalry fought off by a Longbowman on the defense.

        Comment


        • #5
          Ok, here's the deal with *most* random numbers generators.

          Random Number Generators take some arbitary seed, like, say

          0.56876123876

          They run this through a big old function and get a result out, say

          0.75686786767

          (If a program wants to simule a six sided dice, he'll multiple the result from the random number generator by six, add one, and truncate it to an integer. e.g. .756 *6 +1 = 5.536 = 5)

          The *next* time you ask the program for a random number, it uses the result of the previous random event or

          0.75686786767 (recognize this?)

          It'll then get out a number like,

          0.14438612362

          This will, in turn, be the random number seed for the nex call to the random number generator.

          The point is that the sequence of numbers you get out of a random number generator is 100% deterministic. If you start with the same seed, you will *always* get the same set of numbers out of the generator.

          Random number generators are thus not random at all, they're just arbitrary. By picking a seed, you're picking one of a vast, but finite number of possible strings of results.

          Civ III saves the random number seed, every time you save the game. It assigns a *new* seed every time your turn ends. While your turn progresses though, it uses the value of the previous computation as its seed. If you *load* a game, you also load the random number seed the same way you did before.

          The upshot of this is, the die rolls you will get in combat are 100% determined the moment your turn begins.

          Thus if you save the game, and your elite cavalry gets killed by a conscript warrior, you might rationally say "**** that, I'll just load the game and run the combat again." When you load the game, it also reloads your random number seed. So you can run that fight over an infinite number of times and you'll always lose.

          Of course, what is actually saves is a set of dice rolls which says whoever attacks next will get ****ty rolls.

          So we knew R1 == Attacker loses

          So, you can, for example, load the game, have one of your warriors launch a suicide attack. We can then have our cavalry attack the warrior with the turn's *second* set of dice rolls.

          If he wins, we can then start to build a list

          R1 == Attacker Loses
          R2 == Attacker wins

          If you have enough time on your hand, you can map out the whole sequence of dice rolls, and carefully manage your attack around them. You can use fodder troops or fast, retreat capable units, to eat the bad rolls, and use the good rolls for important attacks.

          *Critical Note* the DICE ROLLS, not combat outcomes are deterministic.

          Lets say our dice rolls are :

          0.1
          0.1
          0.1
          0.1
          .99
          .99
          .99
          .99
          .99

          Vetran Cavalry and elite warrior plan to attack a spearman in the open.

          Case 1 :

          Cavalry attacks first.

          Cavalry rolls 4 attacks at 0.1, fails on each one, and dies.
          Warrior then attacks, rolls 5 attacks at .99, hits with each one and wins.

          Case 2 :

          Warrior attacks first.

          Warrior rolls 4 attacks at 0.1 and fails on all 4, taking it to one hp. It then gets lucky and rolls 5 attacks at .99, hitting with each one, and wins.

          In case 1, the result was first attack loses, second attack wins. In case 2, the first attack won. Different units can behave differently with the same dice rolls.

          Thus a string of rolls which means your cavarly loses to an entrenched rifleman might let your battleship beat up a caravel. Alternately, a string of rolls which let your cavalry kill a longbowman in the open doesn't mean if you load the game and attack an entrenched mech infantry with that same cavalry you'll win.
          So be wary of trying to work out the dice rolls themselves. Its a possible, but extremely tedious process. Its generally enough to know that you saved the game right before a bad patch of rolls so need to throw some fodder into the combat until you get into a part of the die roll sequence that is more favorable.

          Comment


          • #6
            So if you want to nitpick about it, you can dig in, attack, then stop and load if you hit a bad roll, which you think the opponent should take?

            Seriously, what other events reset the die roll, though? If you have two groups of different unit types, I don't think one resets the other's rolls. But what about, say, bombardment?

            Also, what resets the seed for random events? I don't think those (ex. defection) are determined at the start of the turn.

            Comment


            • #7
              A very good explanation. Well done.

              One point though
              "It assigns a *new* seed every time your turn ends. "

              I don't believe this is the case. I think it appears to change because random numbers are used during the AIs part of the turn. I base this on observations Very early in the game, when the need for random numbers is minimal. When I was testing to learn the mechanics of the game, during the first 10 turns waiting a turn or two to open a hut, doesn't always change the outcome. This leads me to believe that the seed isn't changed between turns unless a random number is needed to resolve something during the AI's turn. Later in the game waiting a turn will almost always change the hut.

              And in all honesty, from a programming standpoint, since you're already using a random number generator, why would you feel the need write the extra step to change the seed?

              RAH
              It's almost as if all his overconfident, absolutist assertions were spoonfed to him by a trusted website or subreddit. Sheeple
              RIP Tony Bogey & Baron O

              Comment


              • #8
                Originally posted by rah
                I think it appears to change because random numbers are used during the AIs part of the turn. I base this on observations Very early in the game, when the need for random numbers is minimal. When I was testing to learn the mechanics of the game, during the first 10 turns waiting a turn or two to open a hut, doesn't always change the outcome. This leads me to believe that the seed isn't changed between turns unless a random number is needed to resolve something during the AI's turn. Later in the game waiting a turn will almost always change the hut.
                RAH
                I agree. I used to think that the RNG was reseeded every turn, though. This was based on the fact that if you reload before the end of a turn, the AI attacks on your forces will have wildly different outcomes. However, I recently discovered that this is not neccessarily the case.

                I was playing an OCC game when France decided to put me in my place with their modern armor. One tank got into my territory and was defeated by a rifleman. This was game over; there was no way to stop the war now that it had begun. So, I reloaded from the turn before the sneak attack and tried bribing the French. It didn't work and the tank attacked again, and was defeated again. I did this five or six times, each time the rifleman won in exactly the same fashion. Thus, it stands to reason that the RNG is not reseeded each turn.

                Comment


                • #9
                  Originally posted by Excelsior


                  I agree. I used to think that the RNG was reseeded every turn, though. This was based on the fact that if you reload before the end of a turn, the AI attacks on your forces will have wildly different outcomes. However, I recently discovered that this is not neccessarily the case.

                  I was playing an OCC game when France decided to put me in my place with their modern armor. One tank got into my territory and was defeated by a rifleman. This was game over; there was no way to stop the war now that it had begun. So, I reloaded from the turn before the sneak attack and tried bribing the French. It didn't work and the tank attacked again, and was defeated again. I did this five or six times, each time the rifleman won in exactly the same fashion. Thus, it stands to reason that the RNG is not reseeded each turn.
                  I belive that the RNG is not reseeded every turn, but keep in mind that the french attack might have been in the same turn. If the french are plaing their turn after you play yours, then the action takes in the same turn.
                  In civ 1&2 the order was determined by the color if the civ, i.e.: Romans/Russians->Zulus/Babylonians->German/French->Egyptians/Aztecs->Americans/Chinese->English/Greeks->Mongols/Indians. I don't know the order in civ3,but it must have some sort of quenue like this. So, depending on the civ you are playing if the RNG is reseeded every turn, you can get both types of results.

                  Comment


                  • #10
                    Originally posted by Bilo
                    I belive that the RNG is not reseeded every turn, but keep in mind that the french attack might have been in the same turn. If the french are plaing their turn after you play yours, then the action takes in the same turn.
                    Good point. I don't know what determines the play order in Civ3 either.

                    Comment


                    • #11
                      Well... in C++, generating a random number requires the use of the value of the system time. Here is an example for those who are somewhat program savvy, this is a function from part of a random number guessing game I made:

                      int fRandomNumber (int vGenNum)
                      {
                      int vGenValue;
                      int vRandValue;

                      /* Seed the random-number generator with current time so that
                      * the numbers will be different every time we run.
                      */
                      srand( (unsigned)time( NULL ) );

                      for( vGenValue = 0; vGenValue < 1;vGenValue++ )
                      vRandValue = rand();

                      return vRandValue;
                      }
                      I drink to one other, and may that other be he, to drink to another, and may that other be me!

                      Comment


                      • #12
                        Yeah, that's a standard end user interaction with a random function.

                        The thing is, once you seed it, it uses the value of the previous computation as its ongoing seed value.

                        srand() sets the seed value.
                        The next call to rand() uses that seed value
                        Call #2 to rand() uses the end result of the previous call to rand()

                        The source code for the ANSI commitee's "sample" rand function on a 16 bit system follows.

                        void srand(unsigned int seed)
                        {
                        next = seed;
                        }

                        int rand(void)
                        {
                        next = next * 1103515245 + 12345;
                        return (unsigned int)(next/655536) % 32768;
                        }

                        Note the fact that the function is iteratively linked.

                        Once you set the seed with srand, every future call to rand() is determined because rand N+1 uses the result of Rand N as its seed.

                        Comment


                        • #13
                          I gotta agree with you guys, Nothing makes me cuss like a sailor than when a Veteran or Elite Cavalry on offence loses to a freaking warrior or LongBowman on Defense on a flat terrain that has no defense bonus!

                          I've seen a few cases where my privateer on defense kicks the shia out of an English Man-O-War. I'm like "O-K!!!"

                          I learned the heard way about the randomn number generator not being so random. I know I'm guilty of doing a CTRL-S before a major battle in case a freaking regular spearman (2 defense) takes ou my Modern Battle Tank (20 something offense)

                          If I'm sending a Veteran Cavalry against a regular Rifleman or Musketman, I can accept a lose in the situation. But when I use my artillery to pound the shia out of all the defenders and knock them down to 1 health and 6 point veteran offensive unit fails to beat a 1-3 point defensive unit in a city with no walls?!?! Sorry, I just dont buy that shia!

                          Comment


                          • #14
                            Suspicious Results

                            Well if that is the random number generating fuction they have used, then there is something seriously wrong with the way they are using it for combat in the game.

                            I can see trading hit points for a warrior vs warrior battle, but to have swordsmen die without doing a single point of damage on a warrior is a bit much to swallow. I play games like Warhammer 40k and have seen battles turn on the roll of a single die. But with dice, the laws of probability still apply.

                            Because the combat is so bad, I find that nothing I do in the way of strategic planning matters nor does the defensive units I build matter as I know as soon as the AI neighbours declare war, my game is nearly over. Taking the offensive against the AI has worked in 2 games out of about 60 that I have played past 1 AD. In all the others, I have faced multiple enemies or have been out settled because of a lousy starting location. It seems that there should be a balance between all the niceties of diplomacy and trade, and with the need to go to war for living space. In this game, there is no recourse: if you don't start with some serious advantages in location, you might as well restart the game and hope for better terrain.

                            I am really missing the combat system from CTP2: at least there, you could rely on weight of numbers to wear down an opponent that was in a strong position. Not to mention artillery that actually did damage. It is sickening to need 1000 years in games time because you loose twice as many units in combat in open ground as the enemy and to take that civilization's last size 1 city because your 6 catapults will only do 1 hp damage a unit 1 time in 30 attacks and your elite swordsmen will only help make the defending spearman an elite unit without doing a single hit point of damage.

                            I'm going to start keeping track of how many units I loose in combat and how much damage they did. Should be interesting...
                            "Not the cry, but the flight of the wild duck,
                            leads the flock to fly and follow"

                            - Chinese Proverb

                            Comment

                            Working...
                            X