diff options
Diffstat (limited to 'libc')
| -rw-r--r-- | libc/include/stdlib.h | 3 | ||||
| -rw-r--r-- | libc/stdlib.c | 23 |
2 files changed, 26 insertions, 0 deletions
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 146b4b8..0a3d559 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -2,3 +2,6 @@ /* NOT reentrant! */ char* itoa(int val, int base); + +int rand(void); +void srand(unsigned int); diff --git a/libc/stdlib.c b/libc/stdlib.c index 356fc33..49f61f1 100644 --- a/libc/stdlib.c +++ b/libc/stdlib.c @@ -13,3 +13,26 @@ char* itoa(int val, int base) return &buf[i+1]; } + +static int rand_state = 42; + +/* some constants for the RNG */ +#define A 48271 +#define M 2147483647 +#define Q (M/A) +#define R (M%A) + +int rand(void) +{ + int tmp = A * (rand_state % Q) - R * (rand_state / Q); + if(tmp >= 0) + rand_state = tmp; + else + rand_state = tmp + M; + return rand_state; +} + +void srand(unsigned int seed) +{ + rand_state = seed; +} |