date and time created 88/10/19 15:51:23 by bostic
[unix-history] / usr / src / games / rogue / random.c
CommitLineData
b3afadef
KB
1#ifndef lint
2static char sccsid[] = "@(#)random.c 5.1 (Berkeley) %G%";
3#endif /* not lint */
4
5static long rntb[32] = {
6 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
7 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
8 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
9 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
10 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
11 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
12 0x8999220b, 0x27fb47b9
13};
14
15static long *fptr = &rntb[4];
16static long *rptr = &rntb[1];
17static long *state = &rntb[1];
18static int rand_type = 3;
19static int rand_deg = 31;
20static int rand_sep = 3;
21static long *end_ptr = &rntb[32];
22
23srrandom(x)
24int x;
25{
26 register int i;
27 long rrandom();
28
29 state[0] = (long) x;
30 if (rand_type != 0) {
31 for (i = 1; i < rand_deg; i++) {
32 state[i] = 1103515245 * state[i - 1] + 12345;
33 }
34 fptr = &state[rand_sep];
35 rptr = &state[0];
36 for (i = 0; i < 10 * rand_deg; i++) {
37 (void) rrandom();
38 }
39 }
40}
41
42long
43rrandom()
44{
45 long i;
46
47 if (rand_type == 0) {
48 i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
49 } else {
50 *fptr += *rptr;
51 i = (*fptr >> 1) & 0x7fffffff;
52 if (++fptr >= end_ptr) {
53 fptr = state;
54 ++rptr;
55 } else {
56 if (++rptr >= end_ptr) {
57 rptr = state;
58 }
59 }
60 }
61 return(i);
62}
63
64get_rand(x, y)
65register int x, y;
66{
67 register int r, t;
68 long lr;
69
70 if (x > y) {
71 t = y;
72 y = x;
73 x = t;
74 }
75 lr = rrandom();
76 lr &= (long) 0x00003fff;
77 r = (int) lr;
78 r = (r % ((y - x) + 1)) + x;
79 return(r);
80}
81
82rand_percent(percentage)
83register int percentage;
84{
85 return(get_rand(1, 100) <= percentage);
86}
87
88coin_toss()
89{
90
91 return(((rrandom() & 01) ? 1 : 0));
92}