rnz

From NetHackWiki
Jump to navigation Jump to search
rnz(x)
Probability density function of a call of rnz(100).
Distribution unique
Mean approx. 1.29815x
Standard deviation approx. 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 (the mode of the distribution) turns out not to be the given parameter, but half of that number, as can be seen in the graph to the right. The mean outcome, on the other hand, is about 1.30 times the given parameter.

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.

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:

int
rnz(int i)
{
    long x = (long) i;
    long tmp = 1000L;

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

Probability density function

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

i⋅rne⁡(4)⋅1000+rn2⁡(1000)1000

and the other is

i⋅[rne⁡(4)⋅1000+rn2⁡(1000)1000]−1.

The function is easier to analyze if the expression

1000+rn2⁡(1000)1000

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:

Return Probability
1 3/4
2 3/16
3 3/64
4 3/256
5 1/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) returns rne(4) returns Probability Outcome Range Distribution
1 5 1512 15z 110 to 15 x−22560
1 4 3512 14z 18 to 14 3x−22048
1 3 3128 13z 16 to 13 x−2128
1 2 332 12z 14 to 12 3x−264
1 1 38 1z 12 to 1 3x−28
0 1 38 z 1 to 2 38
0 2 332 2z 2 to 4 364
0 3 3128 3z 3 to 6 1128
0 4 3512 4z 4 to 8 32048
0 5 1512 5z 5 to 10 12560

Where the ranges overlap, the distributions add:

Range Distribution
110 to 18 x−22560
18 to 16 19x−210240
16 to 15 99x−210240
15 to 14 19x−22048
14 to 13 7x−2128
13 to 12 3x−264
12 to 1 3x−28
1 to 2 38
2 to 3 364
3 to 4 7128
4 to 5 192048
5 to 6 9910240
6 to 8 1910240
8 to 10 12560

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

Cumulative distribution function

The integral of the probability density function, which contains the probability that the result will be less than x:

Range Distribution
110 to 18 −x−12560+1256
18 to 16 −19x−110240+164
16 to 15 −99x−110240+116
15 to 14 −19x−12048+31512
14 to 13 −7x−1128+31128
13 to 12 −3x−164+732
12 to 1 −3x−18+78
1 to 2 3x8+18
2 to 3 3x64+2532
3 to 4 7x128+97128
4 to 5 19x2048+481512
5 to 6 99x10240+1516
6 to 8 19x10240+6364
8 to 10 x2560+255256

Mean

If p(x) is the probability density function of a continuous random variable, then the mean of the variable is

∫−∞∞xp(x)d⁡x.

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

Range p(x) xp(x) indefinite integral definite integral
−∞ to 110 0 0 0 0
110 to 18 x−22560 x−12560 ln⁡(x)2560 ln⁡(1/8)−ln⁡(1/10)2560
18 to 16 19x−210240 19x−110240 19ln⁡(x)10240 19(ln⁡(1/6)−ln⁡(1/8))10240
16 to 15 99x−210240 99x−110240 99ln⁡(x)10240 99(ln⁡(1/5)−ln⁡(1/6))10240
15 to 14 19x−22048 19x−12048 19ln⁡(x)2048 19(ln⁡(1/4)−ln⁡(1/5))2048
14 to 13 7x−2128 7x−1128 7ln⁡(x)128 7(ln⁡(1/3)−ln⁡(1/4))128
13 to 12 3x−264 3x−164 3ln⁡(x)64 3(ln⁡(1/2)−ln⁡(1/3))64
12 to 1 3x−28 3x−18 3ln⁡(x)8 −3ln⁡(1/2)8
1 to 2 38 3x8 3x216 916
2 to 3 364 3x64 3x2128 15128
3 to 4 7128 7x128 7x2256 49256
4 to 5 192048 19x2048 19x24096 1714096
5 to 6 9910240 99x10240 99x220480 108920480
6 to 8 1910240 19x10240 19x220480 1335120
8 to 10 12560 x2560 x25120 91280
10 to +∞ 0 0 0 0

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

μ=ln⁡(2)⋅441910240+10231024≈1.29815.

Thus the mean result of a call to rnz(x) is 1.29815x. The mean prayer timeout, then, is 454.351 turns. (Actually, it is 453.787, so the error introduced by treating Z as continuous is quite small.)

Standard deviation

The standard deviation of a variable with probability density function p(x) and mean μ is:

σ=∫−∞∞(x−μ)2p(x)dx.

Since μ 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:

Range f(x) (x−μ)2p(x) indefinite integral definite integral
−∞ to 110 0 0 0 0
110 to 18 x−22560 1−2x−1μ+x−2μ22560 x−2ln⁡(x)μ−x−1μ22560 1/40+2μ2+2ln⁡(8)μ−2ln⁡(10)μ2560
18 to 16 19x−210240 19(1−2x−1μ+x−2μ2)10240 19(x−2ln⁡(x)μ−x−1μ2)10240 19(1/24+2μ2+2ln⁡(6)μ−2ln⁡(8)μ)10240
16 to 15 99x−210240 99(1−2x−1μ+x−2μ2)10240 99(x−2ln⁡(x)μ−x−1μ2)10240 99(1/30+μ2+2ln⁡(5)μ−2ln⁡(6)μ)10240
15 to 14 19x−22048 19(1−2x−1μ+x−2μ2)2048 19(x−2ln⁡(x)μ−x−1μ2)2048 19(1/20+μ2+2ln⁡(4)μ−2ln⁡(5)μ)2048
14 to 13 7x−2128 7(1−2x−1μ+x−2μ2)128 7(x−2ln⁡(x)μ−x−1μ2)128 7(1/12+μ2+2ln⁡(3)μ−2ln⁡(4)μ)128
13 to 12 3x−264 3(1−2x−1μ+x−2μ2)64 3(x−2ln⁡(x)μ−x−1μ2)64 3(1/6+μ2+2ln⁡(2)μ−2ln⁡(3)μ)64
12 to 1 3x−28 3(1−2x−1μ+x−2μ2)8 3(x−2ln⁡(x)μ−x−1μ2)8 3(1/2+μ2−2ln⁡(2)μ)8
1 to 2 38 3(x2−2xμ+μ2)8 3(x33−x2μ+xμ2)8 3(7/3−3μ+μ2)8
2 to 3 364 3(x2−2xμ+μ2)64 3(x33−x2μ+xμ2)64 3(19/3−5μ+μ2)64
3 to 4 7128 7(x2−2xμ+μ2)128 7(x33−x2μ+xμ2)128 7(37/3−7μ+μ2)128
4 to 5 192048 19(x2−2xμ+μ2)2048 19(x33−x2μ+xμ2)2048 19(61/3−9μ+μ2)2048
5 to 6 9910240 99(x2−2xμ+μ2)10240 99(x33−x2μ+xμ2)10240 99(91/3−11μ+μ2)10240
6 to 8 1910240 19(x2−2xμ+μ2)10240 19(x33−x2μ+xμ2)10240 19(296/3−28μ+2μ2)10240
8 to 10 12560 x2−2xμ+μ22560 x33−x2μ+xμ22560 488/3−36μ+2μ22560
10 to +∞ 0 0 0 0

Collecting terms and applying the laws of logarithms gives

1136891409600−μ(1023512+ln⁡(2)44195120)+μ2

and substituting the value of μ gives

4659779926214400−ln⁡(2)45206375242880−ln⁡2(2)19527561104857600.

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

σ=4659779926214400−ln⁡(2)45206375242880−ln⁡2(2)19527561104857600≈1.04424.

The standard deviation of rnz(x), then, is 1.04424x, or 365.483 for an ordinary prayer timeout. (Actually, it is 365.344, so the error introduced by treating Z as continuous is quite small.)