home · contact · privacy
Moved pseudo-random generator into its own library, simplified its interface and...
[plomrogue] / src / rrand.c
diff --git a/src/rrand.c b/src/rrand.c
new file mode 100644 (file)
index 0000000..f790ac1
--- /dev/null
@@ -0,0 +1,23 @@
+#include "rrand.h"
+#include <stdint.h> /* for uint16_t, uint32_t */
+
+
+
+static uint32_t seed = 0;
+
+
+
+extern uint16_t rrand()
+{
+    /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
+    seed = ((seed * 1103515245) + 12345) % 2147483648;
+
+    return (seed >> 16);     /* Ignore less random least significant 16 bits. */
+}
+
+
+
+extern void rrand_seed(uint32_t new_seed)
+{
+    seed = new_seed;
+}