xref: /csrg-svn/games/monop/roll.c (revision 33202)
132260Sbostic /*
232260Sbostic  * Copyright (c) 1987 Regents of the University of California.
3*33202Sbostic  * All rights reserved.
4*33202Sbostic  *
5*33202Sbostic  * Redistribution and use in source and binary forms are permitted
6*33202Sbostic  * provided that this notice is preserved and that due credit is given
7*33202Sbostic  * to the University of California at Berkeley. The name of the University
8*33202Sbostic  * may not be used to endorse or promote products derived from this
9*33202Sbostic  * software without specific prior written permission. This software
10*33202Sbostic  * is provided ``as is'' without express or implied warranty.
1132260Sbostic  */
1232260Sbostic 
1332260Sbostic #ifndef lint
14*33202Sbostic static char sccsid[] = "@(#)roll.c	5.3 (Berkeley) 01/02/88";
15*33202Sbostic #endif /* not lint */
1632260Sbostic 
1732260Sbostic /*
1832260Sbostic  *	This routine rolls ndie nside-sided dice.
1932260Sbostic  */
2032260Sbostic 
2132260Sbostic # define	reg	register
2232260Sbostic 
2332261Sbostic # if !defined(vax) && !defined(tahoe)
2432260Sbostic # define	MAXRAND	32767L
2532260Sbostic 
2632260Sbostic roll(ndie, nsides)
2732260Sbostic int	ndie, nsides; {
2832260Sbostic 
2932260Sbostic 	reg long	tot;
3032260Sbostic 	reg unsigned	n, r;
3132260Sbostic 
3232260Sbostic 	tot = 0;
3332260Sbostic 	n = ndie;
3432260Sbostic 	while (n--)
3532260Sbostic 		tot += rand();
3632260Sbostic 	return (int) ((tot * (long) nsides) / ((long) MAXRAND + 1)) + ndie;
3732260Sbostic }
3832260Sbostic 
3932260Sbostic # else
4032260Sbostic 
4132260Sbostic roll(ndie, nsides)
4232260Sbostic reg int	ndie, nsides; {
4332260Sbostic 
4432260Sbostic 	reg int		tot, r;
4532260Sbostic 	reg double	num_sides;
4632260Sbostic 
4732260Sbostic 	num_sides = nsides;
4832260Sbostic 	tot = 0;
4932260Sbostic 	while (ndie--)
5032260Sbostic 		tot += (r = rand()) * (num_sides / 017777777777) + 1;
5132260Sbostic 	return tot;
5232260Sbostic }
5332260Sbostic # endif
54