rnl

From NetHackWiki
Revision as of 00:29, 5 July 2016 by Nethacker (talk | contribs)
Jump to navigation Jump to search

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.

Formulas

Nethack 3.4.3

See source code in [2]

Let R be a random integer number from 0 to x-1 (inclusively).

If Luck=0, R is returned.

Otherwise, with 1/(50-Luck) chance (since Luck is from -13 to 13, the chance should be between 1/37 and 1/63), R is returned.

Otherwise, if x<=15 and Luck>=-5 (that is, x is not too big and the user is not too unlucky), Luck/3 (truncated towards zero) is subtracted from R, than R is forced into the [0,x-1] interval.

Otherwise, Luck is subtracted from R, after which R is forced into the [0,x-1] interval and returned.

Note this means that for Luck in [-2,2] interval and x<=15, rnl just returns a random integer in the [0,x-1] interval. For Luck of -3 or worse, however, rnl have very tiny chance to return 0 (between 1/(53*x) and 1/(63*x), depending on the Luck). For Luck of 3 or better, the chance to return 0 increases significantly. For example, if x=10, the chance will be 10% for Luck between -2 and 2, about 20% for Luck of 3 to 5, and about 49% for Luck of 12 to 13.

Nethack 3.6.0

See source code in [3]

Let R be a random integer number from 0 to x-1 (inclusively).

If Luck=0, R is returned.

Otherwise, let A be

  • Luck/3 rounded to the closest integer, if x<=15 (note that in this case A is from -4 to 4)
  • Luck, otherwise

If A=0, R is returned.

Otherwise, with chance 1/(37+|A|), R is still returned (note that the chance is always between 1/38 and 1/50).

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

Note this means that for Luck in [-1,1] interval and x<=15, rnl just returns a random integer in the [0,x-1] interval (because A=0). For Luck of -2 or worse, however, rnl have very tiny chance to return 0 (between 1/(38*x) and 1/(50*x), depending on the Luck). For Luck of 2 or better, the chance to return 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

rnl is used for the following purposes:

Situation Code Luck
-11 -8 -5 -2 0 +2 +5 +8 +11
Whether your god will help if you pray with exactly 0 alignment rnl(2)==0[4] 01% 01% 01% 01% 50% 99% 99% 99% 99%
Chance of writing a scroll you have not seen rnl(15)==0[5] .2% .2% .2% .2% 6.7% 13.2% 19.7% 26.2% 32.7%
Chance of writing a scroll you have not seen, as a Wizard rnl(3)==0[6] .8% .8% .9% .9% 33.3% 65.8% 98.3% 98.3% 98.3%
Chance to fail to set a landmine (then it blows up) or bear trap (nothing happens) from a horse when riding while unskilled rnl(10)>5[7] 79% 69% 59% 50% 40% 30% 21% 11% 1%
Chance to fail to set a landmine (then it blows up) or bear trap (nothing happens) from a horse when riding while unskilled, while fumbling or with a cursed trap rnl(10)>3[8] 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 fumbled rnl(10)>5[9] 79% 69% 59% 50% 40% 30% 21% 11% 1%
Chance that an otherwise pickup-able object does not slip free when you use a whip to pick it up rnl(6)==0[10] 0.4% 0.4% 0.4% 0.4% 16.7% 32.9% 49.1% 65.4% 81.8%
Chance of finding secret corridors and doors on each surrounding square when searching rnl(7)==0[11] 0.3% 0.4% 0.4% 0.4% 14.3% 28.2% 42.1% 56.1% 70.0%
Chance of finding secret corridors and doors on each surrounding square when searching, while wearing lenses rnl(5)==0[12] 0.5% 0.5% 0.5% 0.5% 20.0% 39.5% 59.0% 78.5% 98.0%
Chance of finding secret corridors 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[13] 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[14] 0.3% 0.3% 0.3% 0.3% 12.5% 24.7% 36.9% 49.1% 61.3%
Chance that a blessed projectile is not broken when thrown rnl(4)==0[15] 0.6% 0.6% 0.6% 0.7% 25% 49.3% 73.7% 98.1% 98.2%
Chance that you are petrified if your saddle slips off[16] or is detroyed(?), or you are polymorphed and lose stone resistance[17], and are riding a steed that petrifies on touch (likely due to your horse having just polymorphed into a footrice) 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[18] 99.5% 99.5% 99.5% 99.5% 80.0% 60.5% 41.0% 21.5% 2.0%
Chance that blessed equipment is not eroded by rust[19] or water[20] damage, or blessed flammable items are not burned by fire damage[21] rnl(4)==0 0.6% 0.6% 0.6% 0.7% 25% 49.3% 73.7% 98.1% 98.2%
Chance that a monster becomes angry when it steps in a trap set by you[22], or that you trigger a trap when failing to disarm it[23] rnl(5)>0 99.5% 99.5% 99.5% 99.5% 80.0% 60.5% 41.0% 21.5% 2.0%
Chance that a hostile, awake, unfrozen, mindful, nonhuman monster becomes peaceful when you free it from a trap not set by you rnl(10)<8[24] 40.1% 50.8% 60.5% 70.3% 80.0% 89.7% 99.5% 99.5% 99.5%
Chance that a lawful player gets +1 alignment for the above rnl(8)==0 times 1/3[25] 0.1% 0.1% 0.1% 0.1% 4.2% 8.2% 12.3% 16.4% 20.4%
Chance that you get +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[26] 0.7% 0.8% 10.5% 20.2% 30% 39.7% 49.5% 59.3% 69.0%
Chance that a boomerang will break when used as a melee weapon rnl(4)==3[27] 98.2% 98.1% 73.7% 49.3% 25% 0.7% 0.6% 0.6% 0.6%
Chance that a successful joust, against a solid monster, breaks your non-artifact lance (very smooth distribution, 4.7% at Luck -13, .01% at Luck 1) rnl(50)==49, 1/5 of the time[28] 4.71% 3.53% 2.35% 1.18% 0.4% 0.01% 0.01% 0.01% 0.01%
Probability based on non-Luck stats
Chance that you can kick down a door (somewhat smooth distribution) rnl(35)<X where X = avg( str, dex, con) + (dex if Samurai or Monk)[29] Approx (Luck+x)/35 chance[30]
Chance that you can kick down a door, avg(str,dex,con)=10 rnl(35)<10 0.5% 6.2% 14.6% 23.0% 28.6% 34.1% 42.5% 50.9% 59.3%
Chance that you can kick down a door, avg(str,dex,con)=15 rnl(35)<15 12.1% 20.5% 28.9% 37.3% 42.9% 48.4% 56.8% 65.2% 73.6%
Chance that you can kick down a door, Samurai or Monk and avg(str,con)=15, dex=15 rnl(35)<30 54.9% 63.4% 71.8% 80.1% 85.7% 91.3% 99.7% 99.7% 99.7%
Chance that you can open a door (smoother distribution) rnl(20)<X where X = avg( str, dex, con)[31] Approx (Luck+x)/20 chance[32]
Chance that you can open a door, avg(str,dex,con)=10 rnl(20)<10 1% 11% 26% 40% 50% 60% 74% 89% 99%
Chance that you can open a door, avg(str,dex,con)=15 rnl(20)<15 21% 36% 51% 65% 75% 85% 99% 99% 99%
One criterion (the other being alignment-based) for an altar conversion[33] (or attempt[34]) producing a summoned minion. rnl(your level)>6 Impossible before level 8, more likely with low luck, more likely at 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 (smoother distribution) 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 (smoother distribution) 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[35] 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 (smoother distribution) 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 (smoother distribution) rnl(32)>0 99.9% 99.9% 99.9% 99.9% 96.9% 90.8% 81.6% 72.4% 63.2%
Numerical Distributions
Average number of fruit that fall from a tree if you kick it 8-rnl(7)[36] 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[37] 5 5 4.7 4.2 3.5 2.8 2.3 2 2
Amount of gold left from a gold golem's death (average, smooth distribution) 200-rnl(101)[38] 140 143 145 148 150 152 155 157 160
Number of darts/arrows/etc left behind when disarming a trap (average, smooth distribution) 50-rnl(50)[39] 16 18 21 24 25.5 27 30 33 35

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".

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 as the luck number 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 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 00% are 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 00% 00% 00% 00% 10% 10% 10% 10% 10% 49%
-12 00% 00% 00% 00% 10% 10% 10% 10% 10% 49%
-11 00% 00% 00% 00% 10% 10% 10% 10% 10% 49%
-10 00% 00% 00% 10% 10% 10% 10% 10% 10% 39%
-9 00% 00% 00% 10% 10% 10% 10% 10% 10% 39%
-8 00% 00% 00% 10% 10% 10% 10% 10% 10% 39%
-7 00% 00% 10% 10% 10% 10% 10% 10% 10% 29%
-6 00% 00% 10% 10% 10% 10% 10% 10% 10% 29%
-5 00% 00% 10% 10% 10% 10% 10% 10% 10% 29%
-4 00% 10% 10% 10% 10% 10% 10% 10% 10% 20%
-3 00% 10% 10% 10% 10% 10% 10% 10% 10% 20%
-2 00% 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% 00%
3 20% 10% 10% 10% 10% 10% 10% 10% 10% 00%
4 20% 10% 10% 10% 10% 10% 10% 10% 10% 00%
5 29% 10% 10% 10% 10% 10% 10% 10% 00% 00%
6 29% 10% 10% 10% 10% 10% 10% 10% 00% 00%
7 29% 10% 10% 10% 10% 10% 10% 10% 00% 00%
8 39% 10% 10% 10% 10% 10% 10% 00% 00% 00%
9 39% 10% 10% 10% 10% 10% 10% 00% 00% 00%
10 39% 10% 10% 10% 10% 10% 10% 00% 00% 00%
11 49% 10% 10% 10% 10% 10% 00% 00% 00% 00%
12 49% 10% 10% 10% 10% 10% 00% 00% 00% 00%
13 49% 10% 10% 10% 10% 10% 00% 00% 00% 00%

References

  1. rnl in rnd.c
  2. rnl code for Nethack 3.4.3
  3. rnl code for Nethack 3.6.0
  4. pray.c in NetHack 3.4.3, line 828
  5. write.c in NetHack 3.4.3, line 201
  6. write.c in NetHack 3.4.3, line 201
  7. apply.c in NetHack 3.4.3, line 2034
  8. apply.c in NetHack 3.4.3, line 2034
  9. apply.c in NetHack 3.4.3, line 2097
  10. apply.c in NetHack 3.4.3, line 2174
  11. detect.c in NetHack 3.4.3, line 1191
  12. detect.c in NetHack 3.4.3, line 1183
  13. detect.c in NetHack 3.4.3, line 1183
  14. detect.c in NetHack 3.4.3, line 1248
  15. dothrow.c in NetHack 3.4.3, line 1332
  16. worn.c in NetHack 3.4.3, line 741
  17. polyself.c in NetHack 3.4.3, line 485
  18. teleport.c in NetHack 3.4.3, line 606
  19. trap.c in NetHack 3.4.3, line 162
  20. trap.c in NetHack 3.4.3, line 2735
  21. trap.c in NetHack 3.4.3, line 2666
  22. trap.c in NetHack 3.4.3, line 1716
  23. trap.c in NetHack 3.4.3, line 3122
  24. trap.c in NetHack 3.4.3, line 3166
  25. trap.c in NetHack 3.4.3, line 3176
  26. trap.c in NetHack 3.4.3, line 3294
  27. uhitm.c in NetHack 3.4.3, line 587
  28. uhitm.c in NetHack 3.4.3, line 1153
  29. dokick.c in NetHack 3.4.3, line 1046
  30. computed numbers slightly incorrect
  31. lock.c in NetHack 3.4.3, line 562
  32. computed numbers slightly incorrect
  33. pray.c in NetHack 3.4.3, line 1396
  34. pray.c in NetHack 3.4.3, line 1408
  35. pray.c in NetHack 3.4.3, line 1618
  36. dokick.c in NetHack 3.4.3, line 900
  37. dokick.c in NetHack 3.4.3, line 900
  38. mon.c in NetHack 3.4.3, line 278
  39. trap.c in NetHack 3.4.3, line 3274