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