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