aboutsummaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2004-05-03 09:10:52 +0000
committerSimon Tatham <anakin@pobox.com>2004-05-03 09:10:52 +0000
commitaa9a8e8c7eecc2de77690b872931e88951622813 (patch)
treecfca97dbf8e19fc3e62c2f6f6689bca480f5ad21 /random.c
parent6e42ddd31b5ca71f48c6260b01fc49b2451d0a56 (diff)
downloadpuzzles-aa9a8e8c7eecc2de77690b872931e88951622813.zip
puzzles-aa9a8e8c7eecc2de77690b872931e88951622813.tar.gz
puzzles-aa9a8e8c7eecc2de77690b872931e88951622813.tar.bz2
puzzles-aa9a8e8c7eecc2de77690b872931e88951622813.tar.xz
The Windows RNG turns out to only give about 16 bits at a time. This
is (a) pretty feeble, and (b) means that although Net seeds transfer between platforms and still generate the same game, there's a suspicious discrepancy in the typical seed _generated_ by each platform. I have a better RNG kicking around in this code base already, so I'll just use it. Each midend has its own random_state, which it passes to new_game_seed() as required. A handy consequence of this is that initial seed data is now passed to midend_new(), which means that new platform implementors are unlikely to forget to seed the RNG because failure to do so causes a compile error! [originally from svn r4187]
Diffstat (limited to 'random.c')
-rw-r--r--random.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/random.c b/random.c
index e0a179e..664b11c 100644
--- a/random.c
+++ b/random.c
@@ -231,7 +231,7 @@ random_state *random_init(char *seed, int len)
unsigned long random_bits(random_state *state, int bits)
{
- int ret = 0;
+ unsigned long ret = 0;
int n;
for (n = 0; n < bits; n += 8) {
@@ -251,7 +251,13 @@ unsigned long random_bits(random_state *state, int bits)
ret = (ret << 8) | state->databuf[state->pos++];
}
- ret &= (1 << bits) - 1;
+ /*
+ * `(1 << bits) - 1' is not good enough, since if bits==32 on a
+ * 32-bit machine, behaviour is undefined and Intel has a nasty
+ * habit of shifting left by zero instead. We'll shift by
+ * bits-1 and then separately shift by one.
+ */
+ ret &= (1 << (bits-1)) * 2 - 1;
return ret;
}