Add to git.
[dlife.git] / random.h
1 /* DLIFE Copyright (C) 2000 Richard W.M. Jones <rich@annexia.org>
2  * and other authors listed in the ``AUTHORS'' file.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * $Id: random.h,v 1.1 2002/04/05 14:40:28 rich Exp $
19  */
20
21 #ifndef random_h
22 #define random_h
23
24 #include "config.h"
25
26 #include <stdlib.h>
27
28 extern void random_init (void);
29
30 /* Define this to run on a fault-free ``perfect'' CPU. (Use this
31  * only when debugging).
32  */
33 #define PERFECT_CPU 0
34
35 /* Return true 1 in PROBABILITY times. The argument PROBABILITY
36  * is not limited to just powers of 2, but it must be an integer.
37  */
38 extern inline int
39 chance (unsigned probability)
40 {
41 #if !PERFECT_CPU
42   return ((unsigned)random() % probability) == 0;
43 #else
44   return 0;
45 #endif
46 }
47
48 /* Return 32 bits of randomness. */
49 extern inline unsigned
50 get_rand_int ()
51 {
52   return (unsigned)random();
53 }
54
55 /* Return 16 bits of randomness. */
56 extern inline unsigned
57 get_rand_short ()
58 {
59   return (unsigned)random() & 0xffff;
60 }
61
62 /* Return 8 bits of randomness. */
63 extern inline unsigned
64 get_rand_byte ()
65 {
66   return (unsigned)random() & 0xff;
67 }
68
69 #endif /* random_h */