home · contact · privacy
Server/py: Add randomness infrastructure, libplomrogue.so ctypes import.
[plomrogue] / libplomrogue.c
1 #include <stdint.h> /* uint16_t, uint32_t */
2
3
4
5 /* Pseudo-randomness seed for rrand(), set by seed_rrand(). */
6 static uint32_t seed = 0;
7
8
9
10 /* With set_seed set, set seed global to seed_input. In any case, return it. */
11 extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input)
12 {
13     if (set_seed)
14     {
15         seed = seed_input;
16     }
17     return seed;
18 }
19
20
21
22 /* Return 16-bit number pseudo-randomly generated via Linear Congruential
23  * Generator algorithm with some proven constants. Use instead of any rand() to
24   * ensure portability of the same pseudo-randomness across systems.
25  */
26 extern uint16_t rrand()
27 {   /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
28     seed = ((seed * 1103515245) + 12345) % 4294967296;
29     return (seed >> 16); /* Ignore less random least significant bits. */
30 }