]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/random.c
90e9676c148f5729907e9c166469bbcd43958e1e
[bsdgames-darwin.git] / rogue / random.c
1 /*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Timothy C. Stoehr.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)random.c 5.3 (Berkeley) 6/1/90";*/
39 static char rcsid[] = "$Id: random.c,v 1.2 1993/08/01 18:52:18 mycroft Exp $";
40 #endif /* not lint */
41
42 /*
43 * random.c
44 *
45 * This source herein may be modified and/or distributed by anybody who
46 * so desires, with the following restrictions:
47 * 1.) No portion of this notice shall be removed.
48 * 2.) Credit shall not be taken for the creation of this source.
49 * 3.) This code is not to be traded, sold, or used for personal
50 * gain or profit.
51 *
52 */
53
54 static long rntb[32] = {
55 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
56 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
57 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
58 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
59 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
60 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
61 0x8999220b, 0x27fb47b9
62 };
63
64 static long *fptr = &rntb[4];
65 static long *rptr = &rntb[1];
66 static long *state = &rntb[1];
67 static int rand_type = 3;
68 static int rand_deg = 31;
69 static int rand_sep = 3;
70 static long *end_ptr = &rntb[32];
71
72 srrandom(x)
73 int x;
74 {
75 register int i;
76 long rrandom();
77
78 state[0] = (long) x;
79 if (rand_type != 0) {
80 for (i = 1; i < rand_deg; i++) {
81 state[i] = 1103515245 * state[i - 1] + 12345;
82 }
83 fptr = &state[rand_sep];
84 rptr = &state[0];
85 for (i = 0; i < 10 * rand_deg; i++) {
86 (void) rrandom();
87 }
88 }
89 }
90
91 long
92 rrandom()
93 {
94 long i;
95
96 if (rand_type == 0) {
97 i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
98 } else {
99 *fptr += *rptr;
100 i = (*fptr >> 1) & 0x7fffffff;
101 if (++fptr >= end_ptr) {
102 fptr = state;
103 ++rptr;
104 } else {
105 if (++rptr >= end_ptr) {
106 rptr = state;
107 }
108 }
109 }
110 return(i);
111 }
112
113 get_rand(x, y)
114 register int x, y;
115 {
116 register int r, t;
117 long lr;
118
119 if (x > y) {
120 t = y;
121 y = x;
122 x = t;
123 }
124 lr = rrandom();
125 lr &= (long) 0x00003fff;
126 r = (int) lr;
127 r = (r % ((y - x) + 1)) + x;
128 return(r);
129 }
130
131 rand_percent(percentage)
132 register int percentage;
133 {
134 return(get_rand(1, 100) <= percentage);
135 }
136
137 coin_toss()
138 {
139
140 return(((rrandom() & 01) ? 1 : 0));
141 }