Make damage() use roundavg()

Turns damage() into a one-liner.

damage() now uses random() % 32768 in chance() instead of random() %
100 inline, therefore can round differently for the same pseudo-random
number.  Update expected smoke test results accordingly.

Aside: "random() % n" distributes evenly only when n is a power of
two.  100 isn't.  However, because random() yields at least 31 bits,
and 100 is so much smaller than 2^31, the error is vanishingly small.
This commit is contained in:
Markus Armbruster 2012-08-14 21:51:22 +02:00
parent 5bf310e6b0
commit c53158eee0
2 changed files with 5 additions and 12 deletions

View file

@ -163,16 +163,9 @@ nukedamage(struct nchrstr *ncp, int range, int airburst)
int
damage(int amt, int pct)
{
int tmp;
int lost;
if (amt <= 0)
return 0;
tmp = amt * pct;
lost = tmp / 100;
if (roll0(100) < tmp % 100)
lost++;
return amt - lost;
return amt - roundavg(amt * (pct / 100.0));
}
/* asymptotic damage to commodities, efficiency, and sectors */