Source:NetHack 3.4.3/src/rnd.c

From NetHackWiki
(Redirected from Source:Ref/rne)
Jump to: navigation, search

Below is the full text to src/rnd.c from NetHack 3.4.3. To link to a particular line, write [[rnd.c#line123]], for example.

This file contains the functions that power NetHack's RNG.

License[edit]

  1. /*	SCCS Id: @(#)rnd.c	3.4	1996/02/07	*/
  2. /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

Top of file[edit]

  1. #include "hack.h"
  2.  
  3. /* "Rand()"s definition is determined by [OS]conf.h */

See, for example, unixconf.h#line303.

  1. #if defined(LINT) && defined(UNIX)	/* rand() is long... */
  2. extern int NDECL(rand);
  3. #define RND(x)	(rand() % x)
  4. #else /* LINT */
  5. # if defined(UNIX) || defined(RANDOM)
  6. #define RND(x)	(int)(Rand() % (long)(x))
  7. # else
  8. /* Good luck: the bottom order bits are cyclic. */
  9. #define RND(x)	(int)((Rand()>>3) % (x))
  10. # endif
  11. #endif /* LINT */


The rest of the file does not worry about which implementation of Rand() is used, instead using the macro RND(x).

rn2[edit]

  1. #ifdef OVL0
  2.  
  3. int
  4. rn2(x)		/* 0 <= rn2(x) < x */
  5. register int x;
  6. {
  7. #ifdef DEBUG
  8. 	if (x <= 0) {
  9. 		impossible("rn2(%d) attempted", x);
  10. 		return(0);
  11. 	}
  12. 	x = RND(x);
  13. 	return(x);
  14. #else
  15. 	return(RND(x));
  16. #endif
  17. }
  18.  
  19. #endif /* OVL0 */


As line 22's comment suggests, rn2(x) returns an integer greater than or equal to zero and less than x.

This is used in the game in situations requiring a 50/50 chance of an event occurring: rn2(2) outputs 0 or 1 with equal probability.

rnl[edit]

  1. #ifdef OVLB
  2.  
  3. int
  4. rnl(x)		/* 0 <= rnl(x) < x; sometimes subtracting Luck */
  5. register int x;	/* good luck approaches 0, bad luck approaches (x-1) */
  6. {
  7. 	register int i;
  8.  
  9. #ifdef DEBUG
  10. 	if (x <= 0) {
  11. 		impossible("rnl(%d) attempted", x);
  12. 		return(0);
  13. 	}
  14. #endif
  15. 	i = RND(x);
  16.  
  17. 	if (Luck && rn2(50 - Luck)) {
  18. 	    i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
  19. 	    if (i < 0) i = 0;
  20. 	    else if (i >= x) i = x-1;
  21. 	}
  22.  
  23. 	return i;
  24. }
  25.  
  26. #endif /* OVLB */

rnl(x) is the same as rn2, with a chance of a proportion of your Luck being subtracted from the result. This means very lucky characters tend to see lower values and very unlucky characters tend to see higher values.

In-game, this is used to make saving throws against your Luck. For example, blessed armor has a saving throw against being eroded by a trap: if rnl(4) is zero, the armor escapes unscathed[1][2][3]:

First, i is a random integer from 0 to 3. A very lucky character (with +13 Luck) has a rn2(37) chance (36 in 37 ~= 97%) of this being reduced by Luck/3, which in this case rounds to 4. i cannot be reduced below 0, so it is set to 0. So the other 1 time in 37 there will be a 25% chance of i being 0. In total, that's a 145/148 chance (~=98%) that the armor will not be eroded.

In the worst case scenario (a very unlucky character with -13 Luck) there is a 251/252 (~=99.6%) of the armor being eroded.

When you have neutral (0) luck, rnl(x) is equal to rn2(x), so there is a 3/4 (75%) chance of the armor being eroded.

Moral: keep your Luck high.

rnd[edit]

  1. #ifdef OVL0
  2.  
  3. int
  4. rnd(x)		/* 1 <= rnd(x) <= x */
  5. register int x;
  6. {
  7. #ifdef DEBUG
  8. 	if (x <= 0) {
  9. 		impossible("rnd(%d) attempted", x);
  10. 		return(1);
  11. 	}
  12. 	x = RND(x)+1;
  13. 	return(x);
  14. #else
  15. 	return(RND(x)+1);
  16. #endif
  17. }
  18.  
  19. #endif /* OVL0 */

rnd(x) returns an integer from 1 to x, inclusive. This simulates the roll of an x-sided die.

d[edit]

  1. #ifdef OVL1
  2.  
  3. int
  4. d(n,x)		/* n <= d(n,x) <= (n*x) */
  5. register int n, x;
  6. {
  7. 	register int tmp = n;
  8.  
  9. #ifdef DEBUG
  10. 	if (x < 0 || n < 0 || (x == 0 && n != 0)) {
  11. 		impossible("d(%d,%d) attempted", n, x);
  12. 		return(1);
  13. 	}
  14. #endif
  15. 	while(n--) tmp += RND(x);
  16. 	return(tmp); /* Alea iacta est. -- J.C. */
  17. }
  18.  
  19. #endif /* OVL1 */
Probability graph of a roll of 3d6 and a normal distribution with the same mean and standard deviation.

d is NetHack's implementation of D notation from D&D. d(n,x) is equivalent to ndx (e.g. d(3,6) is like 3d6). This simulates the roll of n x-sided dice.

"Alea iacta est" is Latin for "the die is cast". "J.C." refers to Julius Caesar, from whom this quote originates.

rne[edit]

  1. #ifdef OVLB
  2.  
  3. int
  4. rne(x)
  5. register int x;
  6. {
  7. 	register int tmp, utmp;
  8.  
  9. 	utmp = (u.ulevel < 15) ? 5 : u.ulevel/3;
  10. 	tmp = 1;
  11. 	while (tmp < utmp && !rn2(x))
  12. 		tmp++;
  13. 	return tmp;
  14.  
  15. 	/* was:
  16. 	 *	tmp = 1;
  17. 	 *	while(!rn2(x)) tmp++;
  18. 	 *	return(min(tmp,(u.ulevel < 15) ? 5 : u.ulevel/3));
  19. 	 * which is clearer but less efficient and stands a vanishingly
  20. 	 * small chance of overflowing tmp
  21. 	 */
  22. }
Probability graph of a call of rne(3) when experience level is less than 18.

rne returns an exponentially weighted random integer from 1 to a number depending on your experience level. It is used in-game for deciding the enchantment of randomly generated items.

rnz[edit]

  1. int
  2. rnz(i)
  3. int i;
  4. {
  5. #ifdef LINT
  6. 	int x = i;
  7. 	int tmp = 1000;
  8. #else
  9. 	register long x = i;
  10. 	register long tmp = 1000;
  11. #endif
  12. 	tmp += rn2(1000);
  13. 	tmp *= rne(4);
  14. 	if (rn2(2)) { x *= tmp; x /= 1000; }
  15. 	else { x *= 1000; x /= tmp; }
  16. 	return((int)x);
  17. }
  18.  
  19. #endif /* OVLB */
  20.  
  21. /*rnd.c*/
Probability graph of a call of rnz(100).

rnz produces a very strange distribution and is used for calculating prayer timeouts, among other things.

See Also[edit]

hack.h#line291 - rn1(x,y) #define

References[edit]

  1. trap.c, line 162
  2. trap.c, line 2666
  3. trap.c, line 2723