]> git.pond.sub.org Git - empserver/blob - src/lib/gen/chance.c
2c647d53a4dfd02fe6e4cb32958e40b121a2cf78
[empserver] / src / lib / gen / chance.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  chance.c: Roll dice
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2006-2012
31  */
32
33 #include <config.h>
34
35 #include <fcntl.h>
36 #include <math.h>
37 #include <stdlib.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 #include "chance.h"
41 #include "mt19937ar.h"
42
43 /*
44  * Return non-zero with probability @d.
45  */
46 int
47 chance(double d)
48 {
49     return d > genrand_real2();
50 }
51
52 /*
53  * Return non-zero with probability @pct%.
54  */
55 int
56 pct_chance(int pct)
57 {
58     return roll(100) <= pct;
59 }
60
61 static unsigned
62 round_up_to_pow2(unsigned val)
63 {
64     val--;
65     val |= val >> 1;
66     val |= val >> 2;
67     val |= val >> 4;
68     val |= val >> 8;
69     val |= val >> 16;
70     val++;
71     return val;
72 }
73
74 /*
75  * Return a random number in [0..@n-1].
76  * @n must be in [1..2^31-1].
77  */
78 int
79 roll0(int n)
80 {
81     unsigned pow2 = round_up_to_pow2(n);
82     int r;
83
84     do
85         r = genrand_int32() & (pow2 - 1);
86     while (r >= n);
87     return r;
88 }
89
90 /*
91  * Return a random number in [1..@n].
92  * @n must be in [0..2^31-1].
93  */
94 int
95 roll(int n)
96 {
97     return 1 + roll0(n);
98 }
99
100 /*
101  * Round @val to nearest integer (on the average).
102  * @val's fractional part is chance to round up.
103  */
104 int
105 roundavg(double val)
106 {
107     double flr = floor(val);
108     return (int)(flr + chance(val - flr));
109 }
110
111 /*
112  * Seed the pseudo-random number generator with @seed.
113  * The sequence of pseudo-random numbers is repeatable by seeding it
114  * with the same value.
115  */
116 void
117 seed_prng(unsigned seed)
118 {
119     init_genrand(seed);
120 }
121
122 /*
123  * Note: this is DJB's hash function when unsigned is 32 bits and hash
124  * is initially 5381.
125  */
126 static unsigned
127 djb_hash(unsigned hash, void *buf, size_t sz)
128 {
129     unsigned char *bp;
130
131     for (bp = buf; bp < (unsigned char *)buf + sz; bp++)
132         hash = hash * 33 ^ *bp;
133
134     return hash;
135 }
136
137 /*
138  * Pick a reasonably random seed for the pseudo-random number generator.
139  */
140 unsigned
141 pick_seed(void)
142 {
143     int fd;
144     unsigned seed;
145     int got_seed = 0;
146     struct timeval tv;
147     pid_t pid;
148
149     /*
150      * Modern systems provide random number devices, but the details
151      * vary.  On many systems, /dev/random blocks when the kernel
152      * entropy pool has been depleted, while /dev/urandom doesn't.
153      * The former should only be used for generating long-lived
154      * cryptographic keys.  On other systems, both devices behave
155      * exactly the same, or only /dev/random exists.
156      *
157      * Try /dev/urandom first, and if it can't be opened, blindly try
158      * /dev/random.
159      */
160     fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK);
161     if (fd < 0)
162         fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
163     if (fd >= 0) {
164         got_seed = read(fd, &seed, sizeof(seed)) == sizeof(seed);
165         close(fd);
166     }
167
168     if (!got_seed) {
169         /* Kernel didn't provide, fall back to hashing time and PID */
170         gettimeofday(&tv, NULL);
171         seed = djb_hash(5381, &tv, sizeof(tv));
172         pid = getpid();
173         seed = djb_hash(seed, &pid, sizeof(pid));
174     }
175
176     return seed;
177 }