aboutsummaryrefslogtreecommitdiff
path: root/user/lib/libc/rand.c
blob: 711ae8eda8b4718ac1136f15a2d01d37272ae709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdlib.h>

/* Random int between lo and hi inclusive */

/* TODO Fix the rand/srand implementation to use the implementation in the
 * (unused) macro which actually has decent pseudo-randomness properties. (No,
 * you can't just change the mod base and expect it to still work fine...) */

#define RANDOM(lo, hi)                                                        \
    ((lo) +                                                                   \
     (((hi) - (lo) + 1) * (randseed = (randseed * 4096 + 150889) % 714025)) / \
         714025)

static unsigned long long randseed = 123456L;

int rand(void)
{
    randseed = (randseed * 4096 + 150889) % RAND_MAX;
    return randseed;
}

void srand(unsigned int seed) { randseed = seed; }