? libc/stdlib/diff Index: include/stdlib.h =================================================================== RCS file: /home/cvs/avr-libc/avr-libc/include/stdlib.h,v retrieving revision 1.5 diff -u -r1.5 stdlib.h --- include/stdlib.h 9 Sep 2002 22:10:04 -0000 1.5 +++ include/stdlib.h 10 Sep 2002 10:27:15 -0000 @@ -71,8 +71,6 @@ /** Comparision function type for qsort(), just for convenience. */ typedef int (*__compar_fn_t)(const void *, const void *); -#define RAND_MAX 0x7FFFFFFF - #ifndef DOXYGEN #ifndef __ATTR_CONST__ @@ -370,6 +368,33 @@ */ extern double strtod(const char *__nptr, char **__endptr); + +/** Highest number that can be generated by rand(). */ +#define RAND_MAX 0x7FFF + +/** + The rand() function computes a sequence of pseudo-random integers in the + range of 0 to \c RAND_MAX (as defined by the header file ). + + The srand() function sets its argument \c seed as the seed for a new + sequence of pseudo-random numbers to be returned by rand(). These + sequences are repeatable by calling srand() with the same seed value. + + If no seed value is provided, the functions are automatically seeded with + a value of 1. +*/ +extern int rand(void); +/** + Pseudo-random number generator seeding; see rand(). +*/ +extern void srand(unsigned int __seed); + +/** + Variant of rand() that stores the context in the user-supplied + variable located at \c ctx instead of a static library variable + so the function becomes re-entrant. +*/ +extern int rand_r(unsigned long *ctx); /address@hidden/ /address@hidden/ @@ -490,9 +515,7 @@ extern int atexit(void (*)(void)); extern double atof(const char *); extern void *calloc(size_t, size_t); -extern int rand(void); extern void *realloc(void *, size_t); -extern void srand(unsigned int); #endif #ifdef __cplusplus Index: libc/stdlib/rand.c =================================================================== RCS file: /home/cvs/avr-libc/avr-libc/libc/stdlib/rand.c,v retrieving revision 1.1 diff -u -r1.1 rand.c --- libc/stdlib/rand.c 5 Jul 2002 20:38:44 -0000 1.1 +++ libc/stdlib/rand.c 10 Sep 2002 10:27:15 -0000 @@ -27,38 +27,58 @@ * SUCH DAMAGE. * * Posix rand_r function added May 1999 by Wes Peters . + * + * $Id$ */ -#if defined(LIBC_SCCS) && !defined(lint) +/* + * From: static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93"; -#endif /* LIBC_SCCS and not lint */ +*/ -#if 0 -#include -#endif #include -#ifdef TEST -#include -#endif /* TEST */ - static int do_rand(unsigned long *ctx) { - return ((*ctx = *ctx * 1103515245 + 12345) % ((unsigned long int)RAND_MAX + 1)); +#ifdef USE_WEAK_SEEDING +/* + * Historic implementation compatibility. + * The random sequences do not vary much with the seed, + * even with overflowing. + */ + return ((*ctx = *ctx * 1103515245L + 12345L) % + ((unsigned long)RAND_MAX + 1)); +#else /* !USE_WEAK_SEEDING */ +/* + * Compute x = (7^5 * x) mod (2^31 - 1) + * wihout overflowing 31 bits: + * (2^31 - 1) = 127773 * (7^5) + 2836 + * From "Random number generators: good ones are hard to find", + * Park and Miller, Communications of the ACM, vol. 31, no. 10, + * October 1988, p. 1195. + */ + long hi, lo, x; + + hi = *ctx / 127773L; + lo = *ctx % 127773L; + x = 16807L * lo - 2836L * hi; + if (x <= 0) + x += 0x7fffffffL; + return ((*ctx = x) % ((unsigned long)RAND_MAX + 1)); +#endif /* !USE_WEAK_SEEDING */ } int -rand_r(unsigned int *ctx) +rand_r(unsigned long *ctx) { - unsigned long int val = (unsigned long int) *ctx; - *ctx = do_rand(&val); + *ctx = do_rand(&ctx); return (int) *ctx; } -static unsigned long int next = 1; +static unsigned long next = 1; int rand(void) @@ -71,32 +91,4 @@ { next = seed; } - -#ifdef TEST - -main() -{ - int i; - unsigned myseed; - - printf("seeding rand with 0x19610910: \n"); - srand(0x19610910); - - printf("generating three pseudo-random numbers:\n"); - for (i = 0; i < 3; i++) - { - printf("next random number = %d\n", rand()); - } - - printf("generating the same sequence with rand_r:\n"); - myseed = 0x19610910; - for (i = 0; i < 3; i++) - { - printf("next random number = %d\n", rand_r(&myseed)); - } - - return 0; -} - -#endif /* TEST */