1 /* SCCS Id: @(#)rnd.c 3.4 1996/02/07 */
2 /* NetHack may be freely redistributed. See license for details. */
6 /* "Rand()"s definition is determined by [OS]conf.h */
7 #if defined(LINT) && defined(UNIX) /* rand() is long... */
8 extern int NDECL(rand);
9 #define RND(x) (rand() % x)
11 # if defined(UNIX) || defined(RANDOM)
12 #define RND(x) (int)(Rand() % (long)(x))
14 /* Good luck: the bottom order bits are cyclic. */
15 #define RND(x) (int)((Rand()>>3) % (x))
22 rn2(x) /* 0 <= rn2(x) < x */
27 impossible("rn2(%d) attempted", x);
41 rnl(x) /* 0 <= rnl(x) < x; sometimes subtracting Luck */
42 register int x; /* good luck approaches 0, bad luck approaches (x-1) */
48 impossible("rnl(%d) attempted", x);
54 if (Luck && rn2(50 - Luck)) {
55 i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
57 else if (i >= x) i = x-1;
67 rnd(x) /* 1 <= rnd(x) <= x */
72 impossible("rnd(%d) attempted", x);
86 d(n,x) /* n <= d(n,x) <= (n*x) */
92 if (x < 0 || n < 0 || (x == 0 && n != 0)) {
93 impossible("d(%d,%d) attempted", n, x);
97 while(n--) tmp += RND(x);
98 return(tmp); /* Alea iacta est. -- J.C. */
108 register int tmp, utmp;
110 utmp = (u.ulevel < 15) ? 5 : u.ulevel/3;
112 while (tmp < utmp && !rn2(x))
118 * while(!rn2(x)) tmp++;
119 * return(min(tmp,(u.ulevel < 15) ? 5 : u.ulevel/3));
120 * which is clearer but less efficient and stands a vanishingly
121 * small chance of overflowing tmp
134 register long tmp = 1000;
138 if (rn2(2)) { x *= tmp; x /= 1000; }
139 else { x *= 1000; x /= tmp; }