commit 1345509e512142239a7a8e4265121e915e9e645a parent dde31ff4005e93479be77b0cbf7803f43a6c44e1 Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com> Date: Tue, 16 Aug 2022 14:10:30 +0200 libc/rand: Use RAND_MAX instead of hardcoding it Some additional comments are added to clarify the meaning of next. Diffstat:
M | src/libc/stdlib/rand.c | | | 12 | +++++++++--- |
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/libc/stdlib/rand.c b/src/libc/stdlib/rand.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#undef rand + #undef srand +#undef rand static unsigned long next; @@ -11,8 +12,13 @@ srand(unsigned seed) } int -rand(void) /* RAND_MAX assumed to be 32767. */ +rand(void) { + /* + * next is just an arbitrary number + * used to generate a random number + * which is <= RAND_MAX + */ next = next * 1103515245 + 12345; - return (unsigned)(next/65536) % 32768; + return (unsigned)(next/65536) % (RAND_MAX + 1); }