The Random number generator

General feedback and discussion of the game.

Moderator: Forum Moderators

Post Reply
pad300
Posts: 3
Joined: November 6th, 2017, 10:09 pm

The Random number generator

Post by pad300 »

I'm new to the game, and I keep seeing what seem to be REALLY odd random number generator results - like orc attacks going 5 of 5 on an elf in the woods, (60% defense). My math says this should be about a 1% chance of happening (0.4^5), yet it seems to happen often (I've been trying the elf campaign, and in the first scenario, and it's happened on 3 run throughs so far - about 9 rounds of combat total (ie not counting the first 2 rounds of the scenario where you and they are still moving to contact) - and it keeps exposing vulnerable shaman units, as their wolf rider promptly jumps into the spot occupied by the dead unit.

Is there a cheat in the random number generator that generates "lucky" results?
User avatar
Poison
Posts: 171
Joined: August 13th, 2017, 4:54 pm

Re: The Random number generator

Post by Poison »

No there is not, when there's probability involved, in the short run, everything can happen, even the craziest results, you've just been running very unlucky. But in the most cases you should see normal stuff (at least that's what is happening to me). Now regarding your math, the actual math is more complicated because you you also hit and depending on opponents health he might not even attempt an attack so the math is more like "you'll hit/miss and if he hits/misses and then you'll hit/miss and if he misses/hits" etc so you'll see the percentages in damage calculations differ from your way and that's also why you'll see many results usually. Now regarding the 5 out of 5, no unit has five hits so I'll assume you got hit by a grunt (2/2 hits) and an archer (3/3). With your way for simplification, the grunt has a 16% to hit all his strikes which isn't that rare (1 every 6 times). The archer is much rarer ofc but still not 1% (approx 1 every 15 times). These are independent so you shouldn't do the math like this.
pad300
Posts: 3
Joined: November 6th, 2017, 10:09 pm

Re: The Random number generator

Post by pad300 »

Poison wrote:No there is not, when there's probability involved, in the short run, everything can happen, even the craziest results, you've just been running very unlucky. But in the most cases you should see normal stuff (at least that's what is happening to me). Now regarding your math, the actual math is more complicated because you you also hit and depending on opponents health he might not even attempt an attack so the math is more like "you'll hit/miss and if he hits/misses and then you'll hit/miss and if he misses/hits" etc so you'll see the percentages in damage calculations differ from your way and that's also why you'll see many results usually. Now regarding the 5 out of 5, no unit has five hits so I'll assume you got hit by a grunt (2/2 hits) and an archer (3/3). With your way for simplification, the grunt has a 16% to hit all his strikes which isn't that rare (1 every 6 times). The archer is much rarer ofc but still not 1% (approx 1 every 15 times). These are independent so you shouldn't do the math like this.
Yeah, actually, if they're independent events, you can do the math exactly as I described. However, to get to the interesting bit in your reply: " you also hit and depending on opponents health " Can you tell me where this system is described (if it is explained), or can you explain how it works?

WRT to the actual fight (turn 3, -25% to Orc dmg), it generally works like 2 orc grunts come up and attack, hit 4 times 2X7 and 2X8 (one of the orcs is strong) , and then an archer walks up and shoots once for 5 pts, and your unit dies... Then the wolf rider charges into the hole and hits your shaman once or twice.
pad300
Posts: 3
Joined: November 6th, 2017, 10:09 pm

Re: The Random number generator

Post by pad300 »

pad300 wrote:
Poison wrote:No there is not, when there's probability involved, in the short run, everything can happen, even the craziest results, you've just been running very unlucky. But in the most cases you should see normal stuff (at least that's what is happening to me). Now regarding your math, the actual math is more complicated because you you also hit and depending on opponents health he might not even attempt an attack so the math is more like "you'll hit/miss and if he hits/misses and then you'll hit/miss and if he misses/hits" etc so you'll see the percentages in damage calculations differ from your way and that's also why you'll see many results usually. Now regarding the 5 out of 5, no unit has five hits so I'll assume you got hit by a grunt (2/2 hits) and an archer (3/3). With your way for simplification, the grunt has a 16% to hit all his strikes which isn't that rare (1 every 6 times). The archer is much rarer ofc but still not 1% (approx 1 every 15 times). These are independent so you shouldn't do the math like this.
Yeah, actually, if they're independent events, you can do the math exactly as I described. However, to get to the interesting bit in your reply: " you also hit and depending on opponents health " Can you tell me where this system is described (if it is explained), or can you explain how it works?

WRT to the actual fight (turn 3, -25% to Orc dmg), it generally works like 2 orc grunts come up and attack, hit 4 times 2X7 and 2X8 (one of the orcs is strong) , and then an archer walks up and shoots once for 5 pts, and your unit dies... Then the wolf rider charges into the hole and hits your shaman once or twice.

To follow this up, for a nominally 50% chance to hit, two attack sequence, there are 4 possible outcomes (A is a hit, B is a Miss):

AA
AB
BA
BB

Are you suggesting that the internal math has been fiddled such that AA and BB are more likely than AB and BA?
User avatar
Poison
Posts: 171
Joined: August 13th, 2017, 4:54 pm

Re: The Random number generator

Post by Poison »

In the game when you attack a unit, at the bottom right there's a button titled "damage calculation" you can press that and see the percentages, if the units aren't fully healed you'll see even lower percentages for the unit that has the terrain disadvantage (so if you keep your units healthy you survive more :roll: ) If you mean the formulas, no idea where to find those but I'm certain they'll be correct, I couldn't check them anyway as my math are non- existent basically atm ( I used to be good back at my uni days but they are long gone now - I mean I wasn't even remembering that the possibility of the events happening together is 1% :( - btw we were doing a lot of probability back then and the formulas that calculate this are rather easy to construct as far as I remember). Nothing has been fiddled, I only meant that the damage calculator shows the health remaining for the 2 units (attacker -defender), so the results depend on both of them hitting/missing. Most of the times, the highest percentage amounts are what you'll actually see. Sorry if I haven't phrased anything properly, I'm not a native English speaker. A little tip, you shouldn't let your units getting hit from more than 2 hexes in BfW if possible.
Last edited by Poison on November 7th, 2017, 3:35 pm, edited 1 time in total.
User avatar
Vulpine
Posts: 152
Joined: February 17th, 2017, 4:13 pm
Location: Capacitated

Re: The Random number generator

Post by Vulpine »

I thought I read somewhere that the number generator was constructed so every time a unit missed the odds of hitting became slightly greater and vice versa (to a certain extent). But I could be very mistaken.
One foot in my mouth, and one in the grave; that's the way I like it, and that's the way it'll stay.
[shameless_advertising]
Also the author of Elvish Incursion, a short little prologue.
[/shameless_advertising]
Tad_Carlucci
Inactive Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: The Random number generator

Post by Tad_Carlucci »

You must be mistaken. If you did read it, it was probably another comment/question like the one you just made.

The random number generator is Mersenne Twister MT19937. and not biased other than by things like terrain, weapon, time-of-day, etc.

The problem with randomness is most people think they understood it because they took statistics: they usually don't.

If you feel there is something biasing the results you're seeing I suggest you take that as a strong indication you're not considering all the factors. So keep playing, read the Help topics, check the Wiki, and study all the information you're given by the game. You'll get it.
I forked real life and now I'm getting merge conflicts.
NovHak
Posts: 8
Joined: November 7th, 2017, 10:06 pm

Re: The Random number generator

Post by NovHak »

Tad_Carlucci wrote:The random number generator is Mersenne Twister MT19937. and not biased other than by things like terrain, weapon, time-of-day, etc.
Interesting... I guess you didn't implement it yourself but rather use it from a standard library. Do you know what the initial conditions are, i.e. how it's initialised ? Because that can ruin the best RNG in some circumstances.

RNGs are often a hot topic on games heavily depending on randomness, such as Chaos Reborn. One way to address this is to rely on cryptographically secure generators. I know, said like this it looks like the hammer on the fly, but it's not overly difficult to use, and crypto RNGs have properties useful outside the crypto domain. It's just that for things involving money or other sensitive applications, higher quality generators have been designed, but why not use them for gaming as well ?

That may require a more per-OS approach however, such as using CryptGenRandom() on Windows, /dev/random on Linux...
Tad_Carlucci
Inactive Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: The Random number generator

Post by Tad_Carlucci »

We use the system entropy source. And, IIRC, we use Boost's MT implementation (including the access to the system's entropy). No, its not crypto-strong but it's fine for a game.

In my experience PRNGs are a hot topic on games generally due to people falsely thinking they know enough from having taken a quarter or two of Stats to be able to show some sort of error or bias; usually with only a half-dozen or so data points.
I forked real life and now I'm getting merge conflicts.
User avatar
Pentarctagon
Project Manager
Posts: 5562
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: The Random number generator

Post by Pentarctagon »

To add to that, Wesnoth uses Boost's Mersenne Twister implementation starting with 1.13.0:

Code: Select all

* New RNG implementation using boost::mt19937, and boost::random_device for seeds. The boost random library is now a build requirement.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
NovHak
Posts: 8
Joined: November 7th, 2017, 10:06 pm

Re: The Random number generator

Post by NovHak »

Okay, thanks for this very documented reply.
Tad_Carlucci wrote:In my experience PRNGs are a hot topic on games generally due to people falsely thinking they know enough from having taken a quarter or two of Stats to be able to show some sort of error or bias; usually with only a half-dozen or so data points.
Yes I know, getting killed six times in a row can be so frustrating :)

After some checking, boost::random_device uses CryptGenRandom() on Windows and /dev/urandom on Linux, which makes for an appropriate seed.
nauar
Posts: 2
Joined: August 29th, 2011, 10:03 pm

Re: The Random number generator

Post by nauar »

Why not use the SFMT: https://en.wikipedia.org/wiki/Mersenne_Twister#SFMT?

It's more equidistributed than Mersenne Twister. So it will keep randomness without having so much unballanced games.
Tad_Carlucci
Inactive Developer
Posts: 503
Joined: April 24th, 2016, 4:18 pm

Re: The Random number generator

Post by Tad_Carlucci »

iirc it must be one of those supported by Boost and we use MT19937 which has good equidistribution.
I forked real life and now I'm getting merge conflicts.
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Random number generator

Post by The_Gnat »

I like that over the years this topic comes up like clockwork over and over. And every time the dev's patiently explain. That is the sign of good developers haha!

(Also for testing purposes if someone really believes the RNG is biased you could randomly generate numbers (creating a scenario or something similar) and display the results of 10000 RNG rolls.)
Post Reply