rnl

From NetHackWiki
Jump to navigation Jump to search
Not to be confused with rn1.

rnl(x)[1] is a pseudo-random number function used in NetHack when a result should be weighted by Luck.

rnl(x) has the same range as rn2(x) (that is, 0 ≤ rnl(x) < x), but is weighted toward 0 if you have positive Luck, and toward x − 1 if you have negative Luck.

Formula

See source code in [1]

Let R be a random integer number from 0 to x − 1 (inclusive).

If Luck = 0, R is returned.

Otherwise, let A be

  • Luck3 rounded to the closest integer, if x ≤ 15 (in this case A ranges from −4 to 4)
  • Luck, otherwise

If A = 0, R is returned.

Otherwise, with chance 137+|A|, R is still returned (the chance is always between 138 and 150).

Otherwise, R − A is forced into the interval [0, x − 1] and returned.

This means that for Luck in the interval [−1,1] and x ≤ 15, rnl just returns a random integer in the interval [0, x − 1] (because A = 0). For Luck of −2 or worse, however, rnl has a tiny chance of returning 0 (between 138x and 150x, depending on Luck). For Luck of 2 or better, the chance of returning 0 increases significantly. For example, if x = 10, the chance will be 10% for Luck between −1 and 1, about 20% for Luck of 2 to 4, and about 49% for Luck of 11 to 13.

How it's used

In the tables below, some very high (99.8%) and very low (0.2%) values above are rounded to 100% or 0%. Nothing is completely 100% or 0% likely, except where spelled out as "certain" or "impossible".

Events based solely on Luck

Situation Code Luck
−11 −8 −5 −2 0 +2 +5 +8 +11
Chance of your god helping you if you pray with exactly 0 alignment rnl(2)==0[2] 1% 1% 1% 1% 50% 99% 99% 99% 99%
Chance of writing a scroll you have not seen rnl(15)==0[3] 0.2% 0.2% 0.2% 0.2% 6.7% 13.2% 19.7% 26.2% 32.7%
Chance of writing a scroll you have not seen, as a Wizard rnl(5)==0[4] 0.5% 0.5% 0.5% 0.5% 20.0% 39.5% 59.0% 78.5% 98.0%
Chance of failing to set a land mine (in which case it blows up) or beartrap (nothing happens) while riding and unskilled at riding rnl(10)>5[5] 79% 69% 59% 50% 40% 30% 21% 11% 1%
Chance of failing to set a land mine (in which case it blows up) or beartrap (nothing happens) while riding and unskilled at riding, and fumbling or using a cursed trap rnl(10)>3[6] 99% 89% 79% 70% 60% 50% 41% 31% 21%
Chance that a trap goes off after you set it, if it was cursed or you were fumbling rnl(10)>5[7] 79% 69% 59% 50% 40% 30% 21% 11% 1%
Chance of snagging an item you can pick up when using a bullwhip (instead of having it slip free) rnl(6)==0[8] 0.4% 0.4% 0.4% 0.4% 16.7% 32.9% 49.1% 65.4% 81.8%
Chance of finding secret passages and doors on each surrounding square when searching rnl(7)==0[9] 0.3% 0.4% 0.4% 0.4% 14.3% 28.2% 42.1% 56.1% 70.0%
Chance of finding secret passages and doors on each surrounding square when searching, while wearing a pair of lenses rnl(5)==0[10] 0.5% 0.5% 0.5% 0.5% 20.0% 39.5% 59.0% 78.5% 98.0%
Chance of finding secret passages and doors on each surrounding square when searching, while wielding a +5 Excalibur, or +3 Excalibur and lenses, or any better combination rnl(2)==0[11] 1.2% 1.3% 1.3% 1.3% 50% 98.7% 98.7% 98.8% 98.8%
Chance of finding a hidden trap when searching rnl(8)==0[12] 0.3% 0.3% 0.3% 0.3% 12.5% 24.7% 36.9% 49.1% 61.3%
Chance of a blessed projectile not breaking when thrown rnl(4)==0[13] 0.6% 0.6% 0.6% 0.7% 25% 49.3% 73.7% 98.1% 98.2%
Chance of being petrified if your saddle slips off[14] or is destroyed, or you are polymorphed and lose stone resistance,[15] when you are riding a steed that petrifies on touch (likely due to your horse having just polymorphed into a cockatrice) rnl(3)>0 99.2% 99.2% 99.1% 99.1% 66.7% 34.2% 1.7% 1.7% 1.6%
Chance of a random levelport when attempting to controlled levelport (or cancel) while confused rnl(5)>0[16] 99.5% 99.5% 99.5% 99.5% 80.0% 60.5% 41.0% 21.5% 2.0%
Chance of blessed equipment not being eroded by rust[17] or water[18] damage, or blessed flammable items not being burned by fire damage[19] rnl(4)==0 0.6% 0.6% 0.6% 0.7% 25% 49.3% 73.7% 98.1% 98.2%
Chance of a monster becoming angry when it steps in a trap set by you,[20] or of you triggering a trap when failing to disarm it[21] rnl(5)>0 99.5% 99.5% 99.5% 99.5% 80.0% 60.5% 41.0% 21.5% 2.0%
Chance of a hostile, awake, unfrozen, mindful, nonhuman monster becoming peaceful when you free it from a trap not set by you rnl(10)<8[22] 40.1% 50.8% 60.5% 70.3% 80.0% 89.7% 99.5% 99.5% 99.5%
Chance of a lawful player getting +1 alignment for the above rnl(8)==0, one-third of the time[23] 0.1% 0.1% 0.1% 0.1% 4.2% 8.2% 12.3% 16.4% 20.4%
Chance of getting +1 alignment for trying and failing to lift a hostile, mobile, mindful, nonhuman monster out of a pit not dug by you rnl(10)<3[24] 0.7% 0.8% 10.5% 20.2% 30% 39.7% 49.5% 59.3% 69.0%
Chance of having a boomerang break when used as a melee weapon rnl(4)==3[25] 98.2% 98.1% 73.7% 49.3% 25% 0.7% 0.6% 0.6% 0.6%
Chance of having a non-artifact lance break when successfully jousting a solid monster rnl(50)==49, 15 of the time[26] 4.71% 3.53% 2.35% 1.18% 0.4% 0.01% 0.01% 0.01% 0.01%

Events based on Luck and other stats

Situation Code Luck
−11 −8 −5 −2 0 +2 +5 +8 +11
Chance of kicking down a door rnl(35)<X, where X = avg(str, dex, con) + (dex if Samurai or Monk)[27] Approx Luck + X35 chance
Chance of kicking down a door when avg(str, dex, con) is 10 rnl(35)<10 0.5% 6.2% 14.6% 23.0% 28.6% 34.1% 42.5% 50.9% 59.3%
Chance of kicking down a door when avg(str, dex, con) is 15 rnl(35)<15 12.1% 20.5% 28.9% 37.3% 42.9% 48.4% 56.8% 65.2% 73.6%
Chance of kicking down a door as a Samurai or Monk when avg(str, con) is 15 and dex is 15 rnl(35)<30 54.9% 63.4% 71.8% 80.1% 85.7% 91.3% 99.7% 99.7% 99.7%
Chance of opening a door rnl(20)<X, where X = avg(str, dex, con)[28] Approx Luck + X20 chance[29]
Chance of opening a door when avg(str, dex, con) is 10 rnl(20)<10 1% 11% 26% 40% 50% 60% 74% 89% 99%
Chance of opening a door when avg(str, dex, con) is 15 rnl(20)<15 21% 36% 51% 65% 75% 85% 99% 99% 99%
One criterion (the other being alignment-based) for a minion to be summoned when converting[30] (or attempting to convert[31]) an altar rnl(your level)>6 Impossible before level 8, more likely with low luck or higher level
Chance of summoning a minion, all other criteria being met, at level 8 rnl(8)>6 61.3% 49.1% 36.9% 24.7% 12.5% 0.3% 0.3% 0.3% 0.3%
Chance of summoning a minion, all other criteria being met, at level 12 rnl(12)>6 74.2% 66.0% 58.0% 49.8% 41.7% 33.6% 25.4% 17.3% 9.1%
Chance of summoning a minion, all other criteria being met, at level 16 rnl(16)>6 99.1% 99.0% 86.8% 68.4% 56.3% 44.1% 25.7% 7.4% 1.2%
Chance of summoning a minion, all other criteria being met, at level 24 rnl(24)>6 99.4% 99.4% 91.2% 79.0% 70.8% 62.7% 50.5% 38.2% 26.0%
Chance of angering your god by praying in Gehennom rnl(alignment)>0[32] Certain with negative alignment, impossible at alignment 1, more likely with higher alignment, less likely with higher luck
Chance of angering your god by praying in Gehennom at alignment 2 rnl(2)>0 98.8% 98.8% 98.7% 98.7% 50% 1.3% 1.3% 1.3% 1.2%
Chance of angering your god by praying in Gehennom at alignment 4 rnl(4)>0 99.4% 99.4% 99.3% 99.3% 75% 50.7% 26.3% 1.9% 1.8%
Chance of angering your god by praying in Gehennom at alignment 8 rnl(8)>0 99.7% 99.7% 99.7% 99.7% 87.5% 75.3% 63.1% 50.9% 38.7%
Chance of angering your god by praying in Gehennom at alignment 16 rnl(16)>0 99.9% 99.9% 99.9% 99.9% 93.8% 81.6% 63.2% 44.9% 26.4%
Chance of angering your god by praying in Gehennom at alignment 32 rnl(32)>0 99.9% 99.9% 99.9% 99.9% 96.9% 90.8% 81.6% 72.4% 63.2%

Numerical distributions

Value Code Luck
−11 −8 −5 −2 0 +2 +5 +8 +11
Average number of fruit that fall from a tree when you kick it 8-rnl(7)[33] 2.5 2.9 3.5 4.2 5 5.8 6.5 7.1 7.5
Average number of bees that are summoned from a tree if you kick it rnl(4)+2[34] 5 5 4.7 4.2 3.5 2.8 2.3 2 2
Average number of gold pieces dropped by a gold golem on death 200-rnl(101)[35] 140 143 145 148 150 152 155 157 160
Average number of darts or arrows left behind when disarming a dart trap or arrow trap 50-rnl(50)[36] 16 18 21 24 25.5 27 30 33 35

Mathematical analysis

Basically, rnl(x) generates a number between 0 and x − 1, and adjusts it in proportion to the player's Luck, with a small chance (which is smaller when Luck tends toward the extremes) of leaving it alone. The net effect is that with positive or negative Luck, the highest or lowest numbers on the list become vanishingly unlikely, and the odds of getting the best and worst result increase, while the odds of getting every result in between stays the same.

Further complicating the matter is that, when x is 15 or less, the adjustment is divided by 3, which means that for those calls there is a stepwise distribution where the odds of everything are almost completely the same within blocks of 3 numbers (−13 to −11, −10 to −8, −7 to −5, −4 to −2, −1 to 1, 2 to 4, 5 to 7, 8 to 10, 11 to 13). Larger parameters have smoother distributions.

In addition, the function often being called in unusual manners, such as comparing it to a non-zero constant, which results in a "slice" being taken from the distribution graph at a less easily explainable point. This is evidenced in the distrubtions for rnl(A)<B or rnl(A)>B above.

Since many of the comparisons are made against 0, having even −2 Luck can cause many results to swing against the player's favor with near certainty. −1 Luck is not as bad as most of these calls also have a small parameter, although it does negatively affect many things that do not call rnl (like prayer or wishing). On the flip side, positive Luck's effect tends to be much less dramatic, and +2 Luck still has a lot of room for improvement. +1 Luck changes almost nothing significantly; the only pronounced effect it has is to cause jousting to be much safer.

Sample distribution

This table is the computed odds of rnl(10) at each possible Luck for each possible result. The "0%" values are actually around 0.24–0.26% depending on the magnitude of the Luck.

Luck Simplified results
0 1 2 3 4 5 6 7 8 9
−13 0% 0% 0% 0% 10% 10% 10% 10% 10% 49%
−12 0% 0% 0% 0% 10% 10% 10% 10% 10% 49%
−11 0% 0% 0% 0% 10% 10% 10% 10% 10% 49%
−10 0% 0% 0% 10% 10% 10% 10% 10% 10% 39%
−9 0% 0% 0% 10% 10% 10% 10% 10% 10% 39%
−8 0% 0% 0% 10% 10% 10% 10% 10% 10% 39%
−7 0% 0% 10% 10% 10% 10% 10% 10% 10% 29%
−6 0% 0% 10% 10% 10% 10% 10% 10% 10% 29%
−5 0% 0% 10% 10% 10% 10% 10% 10% 10% 29%
−4 0% 10% 10% 10% 10% 10% 10% 10% 10% 20%
−3 0% 10% 10% 10% 10% 10% 10% 10% 10% 20%
−2 0% 10% 10% 10% 10% 10% 10% 10% 10% 20%
−1 10% 10% 10% 10% 10% 10% 10% 10% 10% 10%
0 10% 10% 10% 10% 10% 10% 10% 10% 10% 10%
1 10% 10% 10% 10% 10% 10% 10% 10% 10% 10%
2 20% 10% 10% 10% 10% 10% 10% 10% 10% 0%
3 20% 10% 10% 10% 10% 10% 10% 10% 10% 0%
4 20% 10% 10% 10% 10% 10% 10% 10% 0% 0%
5 29% 10% 10% 10% 10% 10% 10% 10% 0% 0%
6 29% 10% 10% 10% 10% 10% 10% 10% 0% 0%
7 29% 10% 10% 10% 10% 10% 10% 10% 0% 0%
8 39% 10% 10% 10% 10% 10% 10% 0% 0% 0%
9 39% 10% 10% 10% 10% 10% 10% 0% 0% 0%
10 39% 10% 10% 10% 10% 10% 10% 0% 0% 0%
11 49% 10% 10% 10% 10% 10% 0% 0% 0% 0%
12 49% 10% 10% 10% 10% 10% 0% 0% 0% 0%
13 49% 10% 10% 10% 10% 10% 0% 0% 0% 0%

History

In NetHack 3.4.3, the formulas were slightly different.[37]

Let R be a random integer number from 0 to x − 1 (inclusive).

If Luck = 0, R is returned.

Otherwise, with 150 − Luck chance (since Luck ranges from −13 to 13, the chance should be between 137 and 163), R is returned.

Otherwise, if x ≤  15 and Luck ≥ −5 (that is, x is not too big and the player is not too unlucky), Luck3 (truncated towards zero) is subtracted from R, then R is forced into the interval [0, x − 1] and returned.

Otherwise, Luck is subtracted from R, then R is forced into the interval [0, x − 1] and returned.

References