diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index b7ed9819..df6cf274 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -29,6 +29,8 @@ typedef struct #define MB_CUR_MAX 4 +#define RAND_MAX 32767 + #ifdef __cplusplus extern "C" { @@ -91,8 +93,11 @@ extern "C" * endptr if nonnull. */ unsigned long strtoul(const char* str, char** endptr, int base); + /* Return the next pseudorandom number. */ int rand(); - void srand(int); + + /* Set the pseudorandom number generator's seed, which will determine the sequence of numbers returned by rand(). */ + void srand(unsigned int seed); /* Exit the program normally, performing any registered cleanup actions. */ __noreturn void exit(int status); diff --git a/libc/src/stdlib.cpp b/libc/src/stdlib.cpp index 59c6bd43..4271aeeb 100644 --- a/libc/src/stdlib.cpp +++ b/libc/src/stdlib.cpp @@ -205,4 +205,17 @@ extern "C" { c_quicksort(base, nmemb, size, compar); } + + static unsigned next = 42; + + int rand() + { + next = next * 1103515245 + 12345; + return (int)((unsigned)(next / 65536) % 32768); + } + + void srand(unsigned int seed) + { + next = seed; + } }