summaryrefslogtreecommitdiff
path: root/libc/stdlib/random.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-03-22 06:14:18 +0000
committerEric Andersen <andersen@codepoet.org>2001-03-22 06:14:18 +0000
commit0a8dc1a9440fc1c4e706f5d346895f67a329ce54 (patch)
treebb05e1c410a1c0ac81ea3f4be160222010060a73 /libc/stdlib/random.c
parentc132ecc38fe4508e9dbd97ec9ada86d6637d97ae (diff)
Add in random(), make rand use that under the hood. Fix the
include file so folks know random is now there.
Diffstat (limited to 'libc/stdlib/random.c')
-rw-r--r--libc/stdlib/random.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/libc/stdlib/random.c b/libc/stdlib/random.c
new file mode 100644
index 000000000..c040ce455
--- /dev/null
+++ b/libc/stdlib/random.c
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+
+/*
+ * This generator is a combination of three linear congruential generators
+ * with periods or 2^15-405, 2^15-1041 and 2^15-1111. It has a period that
+ * is the product of these three numbers.
+ */
+
+static long int seed1 = 1;
+static long int seed2 = 1;
+static long int seed3 = 1;
+
+#define CRANK(a,b,c,m,s) \
+ q = s/a; \
+ s = b*(s-a*q) - c*q; \
+ if(s<0) s+=m;
+
+long int random()
+{
+ register long int q;
+
+ CRANK(206, 157, 31, 32363, seed1);
+ CRANK(217, 146, 45, 31727, seed2);
+ CRANK(222, 142, 133, 31657, seed3);
+
+ return seed1 ^ seed2 ^ seed3;
+}
+
+void srandom(unsigned int seed)
+{
+ seed &= RAND_MAX;
+ seed1 = seed % 32362 + 1;
+ seed2 = seed % 31726 + 1;
+ seed3 = seed % 31656 + 1;
+}
+