TW Probability calculations are incorrect, 0 does not mean 0

Users who are viewing this thread

Not sure where to post this, hopefully someone with contact to the developers can pass this information along.
Since this is happening in multiple places in the bannerlord code base and will lead to unexpected behavior for mods but also for the TW devs itself.

Please find a single example below with an explanation of the error:
C#:
bool isOffspringFemale = (double) MBRandom.RandomFloat <= (double) pregnancyModel.DeliveringFemaleOffspringProbability;

If you look at the above code you might think, that looks quite correct but it's not.
Since the underlying random number generator will return a number in the interval [0, 1)
C#:
/// <summary>Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.</summary>
MBRandom.RandomFloat

For example if you wanted to disable Maternal mortality, you would assume that a probability of 0 would actually mean that this event can never happen.
But because of the way they check that probability there will still be a chance that this event can happen (rng includes 0 in distribution)
This will lead to unexpected behavior for the player / modder and funny bug reports :smile:

For example assuming you would want to disable Maternal mortability, you would actually need to set the propability below 0
Which sucks for the mod UI and player, since they will assume 0 means 0
C#:
bool killMother = MBRandom.RandomFloat <= pregnancyModel.MaternalMortalityProbabilityInLabor
 
The point is that as a dev (mod, native) one would want to avoid unexpected behavior and would want consistent probability calculations.

They could have done the below instead:
C#:
1 - MBRandom.RandomFloat <= pregnancyModel.DeliveringFemaleOffspringProbability;

They have a similar problem elsewhere, now if you always wanted to start a tournament it would never start
because of the rng interval [0, 1)
C#:
MBRandom.RandomFloat >= (double) Campaign.Current.Models.TournamentModel.GetTournamentStartChance(town)

As one can see their pattern for probability calculations are quite inconsistent and it would be useful to make them consistent :smile:
 
Back
Top Bottom