aboutsummaryrefslogtreecommitdiff
path: root/user/lib/libc/rand.c
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /user/lib/libc/rand.c
Created student weenix repository
Diffstat (limited to 'user/lib/libc/rand.c')
-rw-r--r--user/lib/libc/rand.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/user/lib/libc/rand.c b/user/lib/libc/rand.c
new file mode 100644
index 0000000..711ae8e
--- /dev/null
+++ b/user/lib/libc/rand.c
@@ -0,0 +1,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; }