diff options
Diffstat (limited to 'libc/stdlib')
-rw-r--r-- | libc/stdlib/Makefile | 2 | ||||
-rw-r--r-- | libc/stdlib/rand.c | 77 | ||||
-rw-r--r-- | libc/stdlib/random.c | 36 |
3 files changed, 58 insertions, 57 deletions
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; +} + |