# rn2

rn2(x)
Distribution uniform
Mean $(x-1)/2$
Standard deviation $\sqrt{(x^2-1)/12}$

rn2(x)[1] is a pseudo-random number function used in NetHack when a result should be unweighted. The range of values is between 0 and x-1 inclusive (that is, 0 ≤ rn2(x) < x).

For example, to simulate a percentage chance:

if (rn2(100) < chance) { ... }


Also,

!rn2(x)


is true with a 1/x probability.

## References

1. rn2 in rnd.c

It may contain text specific to NetHack 3.4.3. Information on this page may be out of date.

Editors: After reviewing this page and making necessary edits, please change the {{nethack-343}} tag to {{nethack-360}} or {{noversion}} as appropriate.