/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
* chance.c: Roll dice
*
* Known contributors to this file:
- * Markus Armbruster, 2006-2012
+ * Markus Armbruster, 2006-2021
*/
#include <config.h>
#include <fcntl.h>
#include <math.h>
-#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include "mt19937ar.h"
/*
- * Return non-zero with probability D.
+ * Return non-zero with probability @d.
*/
int
chance(double d)
}
/*
- * Return non-zero with probability PCT%.
+ * Return non-zero with probability @pct%.
*/
int
pct_chance(int pct)
}
/*
- * Return a random number in [0..N-1].
- * N must be in [1..2^31-1].
+ * Return a random number in [0..@n-1].
+ * @n must be in [1..2^31-1].
*/
int
roll0(int n)
}
/*
- * Return a random number in [1..N].
- * N must be in [0..2^31-1].
+ * Return a random number in [1..@n].
+ * @n must be in [0..2^31-1].
*/
int
roll(int n)
}
/*
- * Round VAL to nearest integer (on the average).
- * VAL's fractional part is chance to round up.
+ * Round @val to nearest integer (on the average).
+ * @val's fractional part is chance to round up.
*/
int
roundavg(double val)
}
/*
- * Seed the pseudo-random number generator with SEED.
+ * Seed the pseudo-random number generator with @seed.
* The sequence of pseudo-random numbers is repeatable by seeding it
* with the same value.
*/
init_genrand(seed);
}
-static uint32_t
-djb_hash(uint32_t hash, void *buf, size_t sz)
+/*
+ * Note: this is DJB's hash function when unsigned is 32 bits and hash
+ * is initially 5381.
+ */
+static unsigned
+djb_hash(unsigned hash, void *buf, size_t sz)
{
unsigned char *bp;
unsigned
pick_seed(void)
{
- int fd;
- uint32_t seed;
+ unsigned seed;
int got_seed = 0;
struct timeval tv;
pid_t pid;
+#ifndef _WIN32
+ int fd;
/*
* Modern systems provide random number devices, but the details
got_seed = read(fd, &seed, sizeof(seed)) == sizeof(seed);
close(fd);
}
+#endif
if (!got_seed) {
/* Kernel didn't provide, fall back to hashing time and PID */