1 /* NetHack 3.6 rnd.c $NHDT-Date: 1446883921 2015/11/07 08:12:01 $ $NHDT-Branch: master $:$NHDT-Revision: 1.16 $ */
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)))
26 impossible("rn2(%d) attempted", x
);
36 /* 0 <= rnl(x) < x; sometimes subtracting Luck;
37 good luck approaches 0, bad luck approaches (x-1) */
42 register int i
, adjustment
;
46 impossible("rnl(%d) attempted", x
);
53 /* for small ranges, use Luck/3 (rounded away from 0);
54 also guard against architecture-specific differences
55 of integer division involving negative values */
56 adjustment
= (abs(adjustment
) + 1) / 3 * sgn(adjustment
);
62 * -1,0,1 -> 0 (no adjustment)
71 if (adjustment
&& rn2(37 + abs(adjustment
))) {
81 /* 1 <= rnd(x) <= x */
88 impossible("rnd(%d) attempted", x
);
96 /* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */
101 register int tmp
= n
;
104 if (x
< 0 || n
< 0 || (x
== 0 && n
!= 0)) {
105 impossible("d(%d,%d) attempted", n
, x
);
111 return tmp
; /* Alea iacta est. -- J.C. */
114 /* 1 <= rne(x) <= max(u.ulevel/3,5) */
119 register int tmp
, utmp
;
121 utmp
= (u
.ulevel
< 15) ? 5 : u
.ulevel
/ 3;
123 while (tmp
< utmp
&& !rn2(x
))
131 * return min(tmp, (u.ulevel < 15) ? 5 : u.ulevel / 3);
132 * which is clearer but less efficient and stands a vanishingly
133 * small chance of overflowing tmp
137 /* rnz: everyone's favorite! */
146 register long x
= (long) i
;
147 register long tmp
= 1000L;