Source:NetHack 2.3e/rnd.c

From NetHackWiki
Jump to navigation Jump to search

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

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

Screenshots and source code from Hack are used under the CWI license.

1.    /*	SCCS Id: @(#)rnd.c	2.3	87/12/12
2.     */
3.    #include	"config.h"
4.    /* rand() is either random() or lrand48() - see config.h. */
5.    #ifdef UNIX
6.    #define RND(x)	(rand() % (x))
7.    #else
8.    /* Good luck: the bottom order bits are cyclic. */
9.    #define RND(x)	((rand()>>3) % (x))
10.   #endif
11.   
12.   rn1(x,y)	/* y <= rn1(x,y) < (y+x) */ 
13.   register x,y;
14.   {
15.   	return(RND(x)+y);
16.   }
17.   
18.   rn2(x)		/* 0 <= rn2(x) < x */
19.   register x;
20.   {
21.   	return(RND(x));
22.   }
23.   
24.   rnd(x)		/* 1 <= rnd(x) <= x */
25.   register x;
26.   {
27.   	return(RND(x)+1);
28.   }
29.   
30.   d(n,x)		/* n <= d(n,x) <= (n*x) */
31.   register n,x;
32.   {
33.   	register tmp = n;
34.   
35.   	while(n--) tmp += RND(x);
36.   	return(tmp);
37.   }
38.   
39.   rne(x)          /* by stewr 870807 */
40.   register x;
41.   {
42.           register tmp = 1;
43.   	while(!rn2(x)) tmp++;
44.   	return(tmp);
45.   }
46.   
47.   rnz(i)
48.   int i;
49.   {
50.   	register long x = i;
51.           register long tmp = 1000;
52.   	tmp += rn2(1000);
53.   	tmp *= rne(4);
54.   	if (rn2(2)) { x *= tmp; x /= 1000; }
55.   	else { x *= 1000; x /= tmp; }
56.   	return((int)x);
57.   }