Gaming
 

rnz

From Wikihack

rnz(x)
Probability density function of a call of rnz(100).
Distributionunique
Meanapprox. 1.29815x
Standard deviationapprox. 1.04424x

rnz is a function that generates a random number with a distribution that is meant to peak at a given value and trail off at larger values. A call of rnz(350), for example, gives the base prayer timeout and will often return a number close to 350, sometimes a number somewhat less or somewhat more than 350, and rarely a number much greater or much less than 350.

The most probable outcome turns out not to be the given parameter, but half of that number, as can be seen in the graph to the right.

rnz is used for the following purposes:

  • rnz(100) is the time that one must wait after invoking an artifact before invoking it again.
  • rnz(10) (or rnz(25) when first building a level) plays a part in determining when a corpse disintegrates.
  • A number of rnz calls are used in determining the prayer timeout. In particular, the timeout after successful prayer is rnz(350), with a penalty of rnz(1000) added if you are crowned or have killed the Wizard of Yendor. If you have killed the Wizard and are also crowned, the penalty is doubled.

Contents

[edit] Mathematical analysis

rnz produces such a bizarre distribution that it is hard to tell what the original programmer had in mind. It's quite possible that it was meant to be some distribution with Z in the name, but its construction doesn't seem to suggest one.

The code is quoted below, with some cleanup for readability:

int
rnz(i)
int i;
{
        register long x = i;
        register long tmp = 1000;
 
        tmp += rn2(1000);
        tmp *= rne(4);
        if (rn2(2)) { x *= tmp; x /= 1000; }
        else { x *= 1000; x /= tmp; }
        return((int)x);
}


[edit] Probability density function

The rn2(2) call chooses, with equal probability, between two different distributions; one is

math

and the other is

math.

The function is easier to analyze if the expression

math

is modeled as a continuous uniformly distributed variable with range from 1 to 2. Let this variable be called z. For the case where the variable i is divided by the random formula, it helps to know the distribution of the reciprocal of z; it is z-2 for 0.5 <= z <= 1, and 0 otherwise.

The rne(4) call returns an integer from 1 to 5, with the following probabilities:

ReturnProbability
13/4
23/16
33/64
43/256
51/256

If the hero's experience level is 18 or greater, then rne can return numbers greater than 5; but this event has low probability (1/1024 for all experience levels 18 or greater), and to keep this explanation simple it will not be considered.

We can now consider all possible outcomes of the rn2(2) and rne(4) calls:

rn2(2) returnsrne(4) returnsProbabilityOutcomeRangeDistribution
15mathmathmath to mathmath
14mathmathmath to mathmath
13mathmathmath to mathmath
12mathmathmath to mathmath
11mathmathmath to mathmath
01mathmathmath to mathmath
02mathmathmath to mathmath
03mathmathmath to mathmath
04mathmathmath to mathmath
05mathmathmath to mathmath

Where the ranges overlap, the distributions add:

RangeDistribution
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath
math to mathmath

This random variable, multiplied by the parameter i, is the return value of rnz.

[edit] Mean

If math is the probability density function of a continuous random variable, then the mean of the variable is

math

For the continuous random variable described by the table at the end of the previous section, we find:

Rangemathmathindefinite integraldefinite integral
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath
math to mathmathmathmathmath

The sum of the definite integrals looks complicated at first, but by applying the laws of logarithms it can be reduced to

math

Thus the mean result of a call to rnz(x) is 1.29815x. The mean prayer timeout, then, is 454.351 turns.

[edit] Standard deviation

The standard deviation of a variable with probability density function math and mean math is:

math

Since math is a constant, we carry it through the calculation and substitute it at the end. The integral under the square root sign is the variance and we calculate it thus:

Rangemathmathindefinite integraldefinite integral
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to math math math math math
math to mathmathmathmathmath

Collecting terms and applying the laws of logarithms gives

math

and substituting the value of math gives

math

This is the variance of the underlying variable; the standard deviation is its square root:

math

The standard deviation of rnz(x), then, is 1.04424x, or 365.483 for an ordinary prayer timeout.