diff --git a/photon/libc/include/stdlib.h b/photon/libc/include/stdlib.h index afeb2e0..72755fb 100644 --- a/photon/libc/include/stdlib.h +++ b/photon/libc/include/stdlib.h @@ -14,6 +14,9 @@ extern void *realloc(void *ptr, size_t sz); extern void *calloc(size_t n, size_t sz); extern void free(void *ptr); +extern int rand(void); +extern void srand(unsigned int seed); + extern int atoi(const char *str); extern double atof(const char *str); extern long int strtol(const char *str, char **endptr, int base); diff --git a/photon/libc/stdlib/rand.c b/photon/libc/stdlib/rand.c new file mode 100644 index 0000000..81bbf55 --- /dev/null +++ b/photon/libc/stdlib/rand.c @@ -0,0 +1,31 @@ +#include + +static unsigned int seed = 0; + +void srand(unsigned int _seed) +{ + seed = _seed; +} + +int rand(void) +{ + int next = seed; + int result; + + next *= 1103515245; + next += 12345; + result = (int)(next / 65536) % 2048; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (int)(next / 65536) % 1024; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (int)(next / 65536) % 1024; + + seed = next; + return result; +}