xref: /csrg-svn/games/rogue/random.c (revision 36704)
1*36704Sbostic /*
2*36704Sbostic  * Copyright (c) 1988 The Regents of the University of California.
3*36704Sbostic  * All rights reserved.
4*36704Sbostic  *
5*36704Sbostic  * This code is derived from software contributed to Berkeley by
6*36704Sbostic  * Timothy C. Stoehr.
7*36704Sbostic  *
8*36704Sbostic  * Redistribution and use in source and binary forms are permitted
9*36704Sbostic  * provided that the above copyright notice and this paragraph are
10*36704Sbostic  * duplicated in all such forms and that any documentation,
11*36704Sbostic  * advertising materials, and other materials related to such
12*36704Sbostic  * distribution and use acknowledge that the software was developed
13*36704Sbostic  * by the University of California, Berkeley.  The name of the
14*36704Sbostic  * University may not be used to endorse or promote products derived
15*36704Sbostic  * from this software without specific prior written permission.
16*36704Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*36704Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*36704Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*36704Sbostic  */
20*36704Sbostic 
2132689Sbostic #ifndef lint
22*36704Sbostic static char sccsid[] = "@(#)random.c	5.2 (Berkeley) 02/07/89";
2332689Sbostic #endif /* not lint */
2432689Sbostic 
25*36704Sbostic /*
26*36704Sbostic  * random.c
27*36704Sbostic  *
28*36704Sbostic  * This source herein may be modified and/or distributed by anybody who
29*36704Sbostic  * so desires, with the following restrictions:
30*36704Sbostic  *    1.)  No portion of this notice shall be removed.
31*36704Sbostic  *    2.)  Credit shall not be taken for the creation of this source.
32*36704Sbostic  *    3.)  This code is not to be traded, sold, or used for personal
33*36704Sbostic  *         gain or profit.
34*36704Sbostic  *
35*36704Sbostic  */
36*36704Sbostic 
3732689Sbostic static long rntb[32] = {
3832689Sbostic 	         3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
3932689Sbostic 	0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b,
4032689Sbostic 	0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f,
4132689Sbostic 	0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d,
4232689Sbostic 	0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
4332689Sbostic 	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e,
4432689Sbostic 	0x8999220b, 0x27fb47b9
4532689Sbostic };
4632689Sbostic 
4732689Sbostic static long *fptr = &rntb[4];
4832689Sbostic static long *rptr = &rntb[1];
4932689Sbostic static long *state = &rntb[1];
5032689Sbostic static int rand_type = 3;
5132689Sbostic static int rand_deg = 31;
5232689Sbostic static int rand_sep = 3;
5332689Sbostic static long *end_ptr = &rntb[32];
5432689Sbostic 
5532689Sbostic srrandom(x)
5632689Sbostic int x;
5732689Sbostic {
5832689Sbostic 	register int i;
5932689Sbostic 	long rrandom();
6032689Sbostic 
6132689Sbostic 	state[0] = (long) x;
6232689Sbostic 	if (rand_type != 0) {
6332689Sbostic 		for (i = 1; i < rand_deg; i++) {
6432689Sbostic 			state[i] = 1103515245 * state[i - 1] + 12345;
6532689Sbostic 		}
6632689Sbostic 		fptr = &state[rand_sep];
6732689Sbostic 		rptr = &state[0];
6832689Sbostic 		for (i = 0; i < 10 * rand_deg; i++) {
6932689Sbostic 			(void) rrandom();
7032689Sbostic 		}
7132689Sbostic 	}
7232689Sbostic }
7332689Sbostic 
7432689Sbostic long
7532689Sbostic rrandom()
7632689Sbostic {
7732689Sbostic 	long i;
7832689Sbostic 
7932689Sbostic 	if (rand_type == 0) {
8032689Sbostic 		i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff;
8132689Sbostic 	} else {
8232689Sbostic 		*fptr += *rptr;
8332689Sbostic 		i = (*fptr >> 1) & 0x7fffffff;
8432689Sbostic 		if (++fptr >= end_ptr) {
8532689Sbostic 			fptr = state;
8632689Sbostic 			++rptr;
8732689Sbostic 		} else {
8832689Sbostic 			if (++rptr >= end_ptr) {
8932689Sbostic 				rptr = state;
9032689Sbostic 			}
9132689Sbostic 		}
9232689Sbostic 	}
9332689Sbostic 	return(i);
9432689Sbostic }
9532689Sbostic 
9632689Sbostic get_rand(x, y)
9732689Sbostic register int x, y;
9832689Sbostic {
9932689Sbostic 	register int r, t;
10032689Sbostic 	long lr;
10132689Sbostic 
10232689Sbostic 	if (x > y) {
10332689Sbostic 		t = y;
10432689Sbostic 		y = x;
10532689Sbostic 		x = t;
10632689Sbostic 	}
10732689Sbostic 	lr = rrandom();
10832689Sbostic 	lr &= (long) 0x00003fff;
10932689Sbostic 	r = (int) lr;
11032689Sbostic 	r = (r % ((y - x) + 1)) + x;
11132689Sbostic 	return(r);
11232689Sbostic }
11332689Sbostic 
11432689Sbostic rand_percent(percentage)
11532689Sbostic register int percentage;
11632689Sbostic {
11732689Sbostic 	return(get_rand(1, 100) <= percentage);
11832689Sbostic }
11932689Sbostic 
12032689Sbostic coin_toss()
12132689Sbostic {
12232689Sbostic 
12332689Sbostic 	return(((rrandom() & 01) ? 1 : 0));
12432689Sbostic }
125