summaryrefslogtreecommitdiff
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
parentc132ecc38fe4508e9dbd97ec9ada86d6637d97ae (diff)
Add in random(), make rand use that under the hood. Fix the
include file so folks know random is now there.
-rw-r--r--include/stdlib.h5
-rw-r--r--libc/stdlib/Makefile2
-rw-r--r--libc/stdlib/rand.c77
-rw-r--r--libc/stdlib/random.c36
4 files changed, 61 insertions, 59 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 6102eea5a..4f225aacc 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -17,7 +17,7 @@
#define EXIT_SUCCESS 0 /* Successful exit status. */
/* The largest number rand will return */
-#define RAND_MAX INT_MIN
+#define RAND_MAX INT_MAX
/* Maximum length of a multibyte character in the current locale. */
#define MB_CUR_MAX 1
@@ -58,7 +58,8 @@ extern double strtod __P ((const char * nptr, char ** endptr));
/* Random number functions */
extern int rand __P ((void));
extern void srand __P ((unsigned int seed));
-
+extern long int random(void);
+extern void srandom(unsigned int seed);
/* Memory management functions */
extern __ptr_t calloc __P ((size_t, size_t));
diff --git a/libc/stdlib/Makefile b/libc/stdlib/Makefile
index 092de4d94..123841959 100644
--- a/libc/stdlib/Makefile
+++ b/libc/stdlib/Makefile
@@ -38,7 +38,7 @@ MOBJ2=atexit.o exit.o
CSRC = abort.c getenv.c mktemp.c qsort.c realpath.c abs.c bsearch.c \
- mkstemp.c putenv.c rand.c setenv.c system.c div.c ldiv.c
+ mkstemp.c putenv.c rand.c random.c setenv.c system.c div.c ldiv.c
ifeq ($(HAS_FLOATS),true)
CSRC += strtod.c
endif
diff --git a/libc/stdlib/rand.c b/libc/stdlib/rand.c
index b5c5cb764..f8d02cfb4 100644
--- a/libc/stdlib/rand.c
+++ b/libc/stdlib/rand.c
@@ -1,63 +1,28 @@
-#ifdef ZX81_RNG
-/*
- * This is my favorite tiny RNG, If you had a ZX81 you may recognise it :-)
- * (RdeBath)
- */
+/* rand.c
+ *
+ * Written by Erik Andersen <andersee@debian.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; see the file COPYING.LIB. If not,
+ * write to the Free Software Foundation, Inc., 675 Mass Ave,
+ * Cambridge, MA 02139, USA. */
#include <stdlib.h>
-#define MAXINT (((unsigned)-1)>>1)
-
-static unsigned int sseed = 0;
-
-int rand()
-{
- return (sseed = (((sseed + 1L) * 75L) % 65537L) - 1) & MAXINT;
-}
-
-void srand(seed)
-unsigned int seed;
+int rand (void)
{
- sseed = seed;
+ return((int)random());
}
-#else
-
-/*
- * 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 int seed1 = 1;
-static int seed2 = 1;
-static int seed3 = 1;
-
-#define MAXINT (((unsigned)-1)>>1)
-
-#define CRANK(a,b,c,m,s) \
- q = s/a; \
- s = b*(s-a*q) - c*q; \
- if(s<0) s+=m;
-
-int rand()
-{
- register 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 srand(seed)
-unsigned int seed;
-{
- seed &= MAXINT;
- seed1 = seed % 32362 + 1;
- seed2 = seed % 31726 + 1;
- seed3 = seed % 31656 + 1;
-}
+__asm__(".weak srand; srand = srandom");
-#endif
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;
+}
+