Wikia

Wikihack

Source:NetHack 3.0.0/rnd.c

2,032pages on
this wiki
Talk0

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

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
1.    /*	SCCS Id: @(#)rnd.c	3.0	87/07/06
2.     */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include	"hack.h"
6.    
7.    #if defined(LINT) && defined(UNIX)	/* rand() is long... */
8.    extern int rand();
9.    #define RND(x)	(rand() % x)
10.   #else /* LINT */
11.   /* rand() is either random() or lrand48() - see config.h. */
12.   #ifdef UNIX
13.   #define RND(x)	(int)(Rand() % (long)(x))
14.   #else
15.   /* Good luck: the bottom order bits are cyclic. */
16.   #define RND(x)	(int)((Rand()>>3) % (x))
17.   #endif
18.   #endif /* LINT */
19.   
20.   int
21.   rn1(x,y)	/* y <= rn1(x,y) < (y+x) */
22.   register int x, y;
23.   {
24.   	return(RND(x)+y);
25.   }
26.   
27.   int
28.   rn2(x)		/* 0 <= rn2(x) < x */
29.   register int x;
30.   {
31.   	return(RND(x));
32.   }
33.   
34.   int
35.   rnl(x)		/* 0 <= rnl(x) < x; somtimes subtracting Luck */
36.   register x;	/* good luck approaches 0, bad luck approaches (x-1) */
37.   {
38.   	register int i = RND(x);
39.   
40.   	if (Luck && rn2(50 - Luck)) {
41.   	    i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
42.   	    if (i < 0) i = 0;
43.   	    else if (i >= x) i = x-1;
44.   	}
45.   
46.   	return i;
47.   }
48.   
49.   int
50.   rnd(x)		/* 1 <= rnd(x) <= x */
51.   register int x;
52.   {
53.   	return(RND(x)+1);
54.   }
55.   
56.   int
57.   d(n,x)		/* n <= d(n,x) <= (n*x) */
58.   register int n, x;
59.   {
60.   	register int tmp = n;
61.   
62.   	while(n--) tmp += RND(x);
63.   	return(tmp);
64.   }
65.   
66.   int
67.   rne(x)	  /* by stewr 870807 */
68.   register int x;
69.   {
70.   	register int tmp = 1;
71.   	while(!rn2(x)) tmp++;
72.   	return(tmp);
73.   }
74.   
75.   #ifdef THEOLOGY
76.   int
77.   rnz(i)
78.   int i;
79.   {
80.   # ifdef LINT
81.   	int x = i;
82.   	int tmp = 1000;
83.   # else
84.   	register long x = i;
85.   	register long tmp = 1000;
86.   # endif
87.   	tmp += rn2(1000);
88.   	tmp *= rne(4);
89.   	if (rn2(2)) { x *= tmp; x /= 1000; }
90.   	else { x *= 1000; x /= tmp; }
91.   	return((int)x);
92.   }
93.   #endif

Around Wikia's network

Random Wiki