xref: /netbsd/games/rogue/random.c (revision 130a8172)
1*130a8172Sdholland /*	$NetBSD: random.c,v 1.7 2008/01/14 03:50:02 dholland Exp $	*/
27ee35daaScgd 
361f28255Scgd /*
47ee35daaScgd  * Copyright (c) 1988, 1993
57ee35daaScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Timothy C. Stoehr.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
352736b511Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
377ee35daaScgd #if 0
387ee35daaScgd static char sccsid[] = "@(#)random.c	8.1 (Berkeley) 5/31/93";
397ee35daaScgd #else
40*130a8172Sdholland __RCSID("$NetBSD: random.c,v 1.7 2008/01/14 03:50:02 dholland Exp $");
417ee35daaScgd #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd 
442736b511Slukem #include "rogue.h"
452736b511Slukem 
4661f28255Scgd /*
4761f28255Scgd  * random.c
4861f28255Scgd  *
4961f28255Scgd  * This source herein may be modified and/or distributed by anybody who
5061f28255Scgd  * so desires, with the following restrictions:
5161f28255Scgd  *    1.)  No portion of this notice shall be removed.
5261f28255Scgd  *    2.)  Credit shall not be taken for the creation of this source.
5361f28255Scgd  *    3.)  This code is not to be traded, sold, or used for personal
5461f28255Scgd  *         gain or profit.
5561f28255Scgd  *
5661f28255Scgd  */
5761f28255Scgd 
5861f28255Scgd static long rntb[32] = {
5961f28255Scgd 	         3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
6061f28255Scgd 	0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
6161f28255Scgd 	0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
6261f28255Scgd 	0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
6361f28255Scgd 	0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
6461f28255Scgd 	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
6561f28255Scgd 	0x8999220b, 0x27fb47b9
6661f28255Scgd };
6761f28255Scgd 
6861f28255Scgd static long *fptr = &rntb[4];
6961f28255Scgd static long *rptr = &rntb[1];
7061f28255Scgd static long *state = &rntb[1];
7161f28255Scgd static int rand_type = 3;
7261f28255Scgd static int rand_deg = 31;
7361f28255Scgd static int rand_sep = 3;
7461f28255Scgd static long *end_ptr = &rntb[32];
7561f28255Scgd 
762736b511Slukem void
77*130a8172Sdholland srrandom(int x)
7861f28255Scgd {
792736b511Slukem 	int i;
8061f28255Scgd 
81*130a8172Sdholland 	state[0] = x;
8261f28255Scgd 	if (rand_type != 0) {
8361f28255Scgd 		for (i = 1; i < rand_deg; i++) {
8461f28255Scgd 			state[i] = 1103515245 * state[i - 1] + 12345;
8561f28255Scgd 		}
8661f28255Scgd 		fptr = &state[rand_sep];
8761f28255Scgd 		rptr = &state[0];
8861f28255Scgd 		for (i = 0; i < 10 * rand_deg; i++) {
8961f28255Scgd 			(void)rrandom();
9061f28255Scgd 		}
9161f28255Scgd 	}
9261f28255Scgd }
9361f28255Scgd 
9461f28255Scgd long
95*130a8172Sdholland rrandom(void)
9661f28255Scgd {
9761f28255Scgd 	long i;
9861f28255Scgd 
9961f28255Scgd 	if (rand_type == 0) {
10061f28255Scgd 		i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
10161f28255Scgd 	} else {
10261f28255Scgd 		*fptr += *rptr;
10361f28255Scgd 		i = (*fptr >> 1) & 0x7fffffff;
10461f28255Scgd 		if (++fptr >= end_ptr) {
10561f28255Scgd 			fptr = state;
10661f28255Scgd 			++rptr;
10761f28255Scgd 		} else {
10861f28255Scgd 			if (++rptr >= end_ptr) {
10961f28255Scgd 				rptr = state;
11061f28255Scgd 			}
11161f28255Scgd 		}
11261f28255Scgd 	}
11361f28255Scgd 	return(i);
11461f28255Scgd }
11561f28255Scgd 
1162736b511Slukem int
117*130a8172Sdholland get_rand(int x, int y)
11861f28255Scgd {
1192736b511Slukem 	int r, t;
12061f28255Scgd 	long lr;
12161f28255Scgd 
12261f28255Scgd 	if (x > y) {
12361f28255Scgd 		t = y;
12461f28255Scgd 		y = x;
12561f28255Scgd 		x = t;
12661f28255Scgd 	}
12761f28255Scgd 	lr = rrandom();
128*130a8172Sdholland 	lr &= 0x00003fffL;
12961f28255Scgd 	r = (int)lr;
13061f28255Scgd 	r = (r % ((y - x) + 1)) + x;
13161f28255Scgd 	return(r);
13261f28255Scgd }
13361f28255Scgd 
1342736b511Slukem int
135*130a8172Sdholland rand_percent(int percentage)
13661f28255Scgd {
13761f28255Scgd 	return(get_rand(1, 100) <= percentage);
13861f28255Scgd }
13961f28255Scgd 
1402736b511Slukem int
141*130a8172Sdholland coin_toss(void)
14261f28255Scgd {
14361f28255Scgd 	return(((rrandom() & 01) ? 1 : 0));
14461f28255Scgd }
145