xref: /netbsd/games/rogue/random.c (revision 1e99780e)
1*1e99780eSdholland /*	$NetBSD: random.c,v 1.8 2009/08/12 08:44:45 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*1e99780eSdholland __RCSID("$NetBSD: random.c,v 1.8 2009/08/12 08:44:45 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 
76*1e99780eSdholland static long rrandom(void);
77*1e99780eSdholland 
782736b511Slukem void
srrandom(int x)79130a8172Sdholland srrandom(int x)
8061f28255Scgd {
812736b511Slukem 	int i;
8261f28255Scgd 
83130a8172Sdholland 	state[0] = x;
8461f28255Scgd 	if (rand_type != 0) {
8561f28255Scgd 		for (i = 1; i < rand_deg; i++) {
8661f28255Scgd 			state[i] = 1103515245 * state[i - 1] + 12345;
8761f28255Scgd 		}
8861f28255Scgd 		fptr = &state[rand_sep];
8961f28255Scgd 		rptr = &state[0];
9061f28255Scgd 		for (i = 0; i < 10 * rand_deg; i++) {
9161f28255Scgd 			(void)rrandom();
9261f28255Scgd 		}
9361f28255Scgd 	}
9461f28255Scgd }
9561f28255Scgd 
96*1e99780eSdholland static long
rrandom(void)97130a8172Sdholland rrandom(void)
9861f28255Scgd {
9961f28255Scgd 	long i;
10061f28255Scgd 
10161f28255Scgd 	if (rand_type == 0) {
10261f28255Scgd 		i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
10361f28255Scgd 	} else {
10461f28255Scgd 		*fptr += *rptr;
10561f28255Scgd 		i = (*fptr >> 1) & 0x7fffffff;
10661f28255Scgd 		if (++fptr >= end_ptr) {
10761f28255Scgd 			fptr = state;
10861f28255Scgd 			++rptr;
10961f28255Scgd 		} else {
11061f28255Scgd 			if (++rptr >= end_ptr) {
11161f28255Scgd 				rptr = state;
11261f28255Scgd 			}
11361f28255Scgd 		}
11461f28255Scgd 	}
11561f28255Scgd 	return(i);
11661f28255Scgd }
11761f28255Scgd 
1182736b511Slukem int
get_rand(int x,int y)119130a8172Sdholland get_rand(int x, int y)
12061f28255Scgd {
1212736b511Slukem 	int r, t;
12261f28255Scgd 	long lr;
12361f28255Scgd 
12461f28255Scgd 	if (x > y) {
12561f28255Scgd 		t = y;
12661f28255Scgd 		y = x;
12761f28255Scgd 		x = t;
12861f28255Scgd 	}
12961f28255Scgd 	lr = rrandom();
130130a8172Sdholland 	lr &= 0x00003fffL;
13161f28255Scgd 	r = (int)lr;
13261f28255Scgd 	r = (r % ((y - x) + 1)) + x;
13361f28255Scgd 	return(r);
13461f28255Scgd }
13561f28255Scgd 
1362736b511Slukem int
rand_percent(int percentage)137130a8172Sdholland rand_percent(int percentage)
13861f28255Scgd {
13961f28255Scgd 	return(get_rand(1, 100) <= percentage);
14061f28255Scgd }
14161f28255Scgd 
1422736b511Slukem int
coin_toss(void)143130a8172Sdholland coin_toss(void)
14461f28255Scgd {
14561f28255Scgd 	return(((rrandom() & 01) ? 1 : 0));
14661f28255Scgd }
147