X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=libplomrogue.c;fp=libplomrogue.c;h=c6f572416339559cfdfbb35ee7a25a2d64377092;hb=659685982349318d6aaa271259fef324dd5c8d30;hp=0000000000000000000000000000000000000000;hpb=7ea9de2749221e7c5fd5182bd32a386522c9fd65;p=plomrogue diff --git a/libplomrogue.c b/libplomrogue.c new file mode 100644 index 0000000..c6f5724 --- /dev/null +++ b/libplomrogue.c @@ -0,0 +1,30 @@ +#include /* uint16_t, uint32_t */ + + + +/* Pseudo-randomness seed for rrand(), set by seed_rrand(). */ +static uint32_t seed = 0; + + + +/* With set_seed set, set seed global to seed_input. In any case, return it. */ +extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input) +{ + if (set_seed) + { + seed = seed_input; + } + return seed; +} + + + +/* Return 16-bit number pseudo-randomly generated via Linear Congruential + * Generator algorithm with some proven constants. Use instead of any rand() to + * ensure portability of the same pseudo-randomness across systems. + */ +extern uint16_t rrand() +{ /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */ + seed = ((seed * 1103515245) + 12345) % 4294967296; + return (seed >> 16); /* Ignore less random least significant bits. */ +}