Explorar o código

Add in random(), make rand use that under the hood. Fix the
include file so folks know random is now there.

Eric Andersen %!s(int64=23) %!d(string=hai) anos
pai
achega
0a8dc1a944
Modificáronse 4 ficheiros con 61 adicións e 59 borrados
  1. 3 2
      include/stdlib.h
  2. 1 1
      libc/stdlib/Makefile
  3. 21 56
      libc/stdlib/rand.c
  4. 36 0
      libc/stdlib/random.c

+ 3 - 2
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));

+ 1 - 1
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

+ 21 - 56
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

+ 36 - 0
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;
+}
+