/
rnd.c
145 lines (124 loc) · 2.33 KB
/
rnd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* SCCS Id: @(#)rnd.c 3.4 1996/02/07 */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
/* "Rand()"s definition is determined by [OS]conf.h */
#if defined(LINT) && defined(UNIX) /* rand() is long... */
extern int NDECL(rand);
#define RND(x) (rand() % x)
#else /* LINT */
# if defined(UNIX) || defined(RANDOM)
#define RND(x) (int)(Rand() % (long)(x))
# else
/* Good luck: the bottom order bits are cyclic. */
#define RND(x) (int)((Rand()>>3) % (x))
# endif
#endif /* LINT */
#ifdef OVL0
int
rn2(x) /* 0 <= rn2(x) < x */
register int x;
{
#ifdef DEBUG
if (x <= 0) {
impossible("rn2(%d) attempted", x);
return(0);
}
x = RND(x);
return(x);
#else
return(RND(x));
#endif
}
#endif /* OVL0 */
#ifdef OVLB
int
rnl(x) /* 0 <= rnl(x) < x; sometimes subtracting Luck */
register int x; /* good luck approaches 0, bad luck approaches (x-1) */
{
register int i;
#ifdef DEBUG
if (x <= 0) {
impossible("rnl(%d) attempted", x);
return(0);
}
#endif
i = RND(x);
if (Luck && rn2(50 - Luck)) {
i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck);
if (i < 0) i = 0;
else if (i >= x) i = x-1;
}
return i;
}
#endif /* OVLB */
#ifdef OVL0
int
rnd(x) /* 1 <= rnd(x) <= x */
register int x;
{
#ifdef DEBUG
if (x <= 0) {
impossible("rnd(%d) attempted", x);
return(1);
}
x = RND(x)+1;
return(x);
#else
return(RND(x)+1);
#endif
}
#endif /* OVL0 */
#ifdef OVL1
int
d(n,x) /* n <= d(n,x) <= (n*x) */
register int n, x;
{
register int tmp = n;
#ifdef DEBUG
if (x < 0 || n < 0 || (x == 0 && n != 0)) {
impossible("d(%d,%d) attempted", n, x);
return(1);
}
#endif
while(n--) tmp += RND(x);
return(tmp); /* Alea iacta est. -- J.C. */
}
#endif /* OVL1 */
#ifdef OVLB
int
rne(x)
register int x;
{
register int tmp, utmp;
utmp = (u.ulevel < 15) ? 5 : u.ulevel/3;
tmp = 1;
while (tmp < utmp && !rn2(x))
tmp++;
return tmp;
/* was:
* tmp = 1;
* while(!rn2(x)) tmp++;
* return(min(tmp,(u.ulevel < 15) ? 5 : u.ulevel/3));
* which is clearer but less efficient and stands a vanishingly
* small chance of overflowing tmp
*/
}
int
rnz(i)
int i;
{
#ifdef LINT
int x = i;
int tmp = 1000;
#else
register long x = i;
register long tmp = 1000;
#endif
tmp += rn2(1000);
tmp *= rne(4);
if (rn2(2)) { x *= tmp; x /= 1000; }
else { x *= 1000; x /= tmp; }
return((int)x);
}
#endif /* OVLB */
/*rnd.c*/