xref: /csrg-svn/games/rogue/random.c (revision 60842)
136704Sbostic /*
2*60842Sbostic  * Copyright (c) 1988, 1993
3*60842Sbostic  *	The Regents of the University of California.  All rights reserved.
436704Sbostic  *
536704Sbostic  * This code is derived from software contributed to Berkeley by
636704Sbostic  * Timothy C. Stoehr.
736704Sbostic  *
842601Sbostic  * %sccs.include.redist.c%
936704Sbostic  */
1036704Sbostic 
1132689Sbostic #ifndef lint
12*60842Sbostic static char sccsid[] = "@(#)random.c	8.1 (Berkeley) 05/31/93";
1332689Sbostic #endif /* not lint */
1432689Sbostic 
1536704Sbostic /*
1636704Sbostic  * random.c
1736704Sbostic  *
1836704Sbostic  * This source herein may be modified and/or distributed by anybody who
1936704Sbostic  * so desires, with the following restrictions:
2036704Sbostic  *    1.)  No portion of this notice shall be removed.
2136704Sbostic  *    2.)  Credit shall not be taken for the creation of this source.
2236704Sbostic  *    3.)  This code is not to be traded, sold, or used for personal
2336704Sbostic  *         gain or profit.
2436704Sbostic  *
2536704Sbostic  */
2636704Sbostic 
2732689Sbostic static long rntb[32] = {
2832689Sbostic 	         3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
2932689Sbostic 	0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
3032689Sbostic 	0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
3132689Sbostic 	0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
3232689Sbostic 	0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
3332689Sbostic 	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
3432689Sbostic 	0x8999220b, 0x27fb47b9
3532689Sbostic };
3632689Sbostic 
3732689Sbostic static long *fptr = &rntb[4];
3832689Sbostic static long *rptr = &rntb[1];
3932689Sbostic static long *state = &rntb[1];
4032689Sbostic static int rand_type = 3;
4132689Sbostic static int rand_deg = 31;
4232689Sbostic static int rand_sep = 3;
4332689Sbostic static long *end_ptr = &rntb[32];
4432689Sbostic 
srrandom(x)4532689Sbostic srrandom(x)
4632689Sbostic int x;
4732689Sbostic {
4832689Sbostic 	register int i;
4932689Sbostic 	long rrandom();
5032689Sbostic 
5132689Sbostic 	state[0] = (long) x;
5232689Sbostic 	if (rand_type != 0) {
5332689Sbostic 		for (i = 1; i < rand_deg; i++) {
5432689Sbostic 			state[i] = 1103515245 * state[i - 1] + 12345;
5532689Sbostic 		}
5632689Sbostic 		fptr = &state[rand_sep];
5732689Sbostic 		rptr = &state[0];
5832689Sbostic 		for (i = 0; i < 10 * rand_deg; i++) {
5932689Sbostic 			(void) rrandom();
6032689Sbostic 		}
6132689Sbostic 	}
6232689Sbostic }
6332689Sbostic 
6432689Sbostic long
rrandom()6532689Sbostic rrandom()
6632689Sbostic {
6732689Sbostic 	long i;
6832689Sbostic 
6932689Sbostic 	if (rand_type == 0) {
7032689Sbostic 		i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
7132689Sbostic 	} else {
7232689Sbostic 		*fptr += *rptr;
7332689Sbostic 		i = (*fptr >> 1) & 0x7fffffff;
7432689Sbostic 		if (++fptr >= end_ptr) {
7532689Sbostic 			fptr = state;
7632689Sbostic 			++rptr;
7732689Sbostic 		} else {
7832689Sbostic 			if (++rptr >= end_ptr) {
7932689Sbostic 				rptr = state;
8032689Sbostic 			}
8132689Sbostic 		}
8232689Sbostic 	}
8332689Sbostic 	return(i);
8432689Sbostic }
8532689Sbostic 
get_rand(x,y)8632689Sbostic get_rand(x, y)
8732689Sbostic register int x, y;
8832689Sbostic {
8932689Sbostic 	register int r, t;
9032689Sbostic 	long lr;
9132689Sbostic 
9232689Sbostic 	if (x > y) {
9332689Sbostic 		t = y;
9432689Sbostic 		y = x;
9532689Sbostic 		x = t;
9632689Sbostic 	}
9732689Sbostic 	lr = rrandom();
9832689Sbostic 	lr &= (long) 0x00003fff;
9932689Sbostic 	r = (int) lr;
10032689Sbostic 	r = (r % ((y - x) + 1)) + x;
10132689Sbostic 	return(r);
10232689Sbostic }
10332689Sbostic 
rand_percent(percentage)10432689Sbostic rand_percent(percentage)
10532689Sbostic register int percentage;
10632689Sbostic {
10732689Sbostic 	return(get_rand(1, 100) <= percentage);
10832689Sbostic }
10932689Sbostic 
coin_toss()11032689Sbostic coin_toss()
11132689Sbostic {
11232689Sbostic 
11332689Sbostic 	return(((rrandom() & 01) ? 1 : 0));
11432689Sbostic }
115