xref: /original-bsd/games/monop/roll.c (revision 2ce9ec30)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)roll.c	5.3 (Berkeley) 01/02/88";
15 #endif /* not lint */
16 
17 /*
18  *	This routine rolls ndie nside-sided dice.
19  */
20 
21 # define	reg	register
22 
23 # if !defined(vax) && !defined(tahoe)
24 # define	MAXRAND	32767L
25 
26 roll(ndie, nsides)
27 int	ndie, nsides; {
28 
29 	reg long	tot;
30 	reg unsigned	n, r;
31 
32 	tot = 0;
33 	n = ndie;
34 	while (n--)
35 		tot += rand();
36 	return (int) ((tot * (long) nsides) / ((long) MAXRAND + 1)) + ndie;
37 }
38 
39 # else
40 
41 roll(ndie, nsides)
42 reg int	ndie, nsides; {
43 
44 	reg int		tot, r;
45 	reg double	num_sides;
46 
47 	num_sides = nsides;
48 	tot = 0;
49 	while (ndie--)
50 		tot += (r = rand()) * (num_sides / 017777777777) + 1;
51 	return tot;
52 }
53 # endif
54