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 9 Sep 2002 14:12:15 -0000 @@ -26,77 +26,49 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * Posix rand_r function added May 1999 by Wes Peters . + * $Id$ */ -#if defined(LIBC_SCCS) && !defined(lint) -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)); -} - - -int -rand_r(unsigned int *ctx) -{ - unsigned long int val = (unsigned long int) *ctx; - *ctx = do_rand(&val); - return (int) *ctx; -} +/* Origin: "@(#)rand.c 8.1 (Berkeley) 6/14/93" */ +#include -static unsigned long int next = 1; +static unsigned long next = 1; int rand(void) { - return do_rand(&next); +#ifdef USE_WEAK_SEEDING +/* + * Historic implementation compatibility. + * The random sequences do not vary much with the seed, + * even with overflowing. + */ + return ((next = next * 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 = next / 127773L; + lo = next % 127773L; + x = 16807L * lo - 2836L * hi; + if (x <= 0) + x += 0x7fffffffL; + return ((next = x) % ((unsigned long)RAND_MAX + 1)); +#endif /* !USE_WEAK_SEEDING */ } void -srand(unsigned int seed) +srand(unsigned seed) { 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 */ Index: include/stdlib.h =================================================================== RCS file: /home/cvs/avr-libc/avr-libc/include/stdlib.h,v retrieving revision 1.3 diff -u -r1.3 stdlib.h --- include/stdlib.h 9 Sep 2002 09:06:57 -0000 1.3 +++ include/stdlib.h 9 Sep 2002 14:12:15 -0000 @@ -56,7 +56,9 @@ */ /address@hidden/ -/** \name Standard functionality. */ +/** \name Standard functionality. + \ingroup avr_stdlib +*/ /** Result type for function div(). */ typedef struct { int quot; @@ -72,7 +74,8 @@ /** Comparision function type for qsort(), just for convenience. */ typedef int (*__compar_fn_t)(const void *, const void *); -#define RAND_MAX 0x7FFFFFFF +/** Highest number that can be generated by rand(). */ +#define RAND_MAX 0x7FFF #ifndef DOXYGEN @@ -371,10 +374,29 @@ */ extern double strtod(const char *__nptr, char **__endptr); + +/** + 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); /address@hidden/ /address@hidden/ -/** \name Non-standard (i.e. non-ISO C) functions */ +/** \name Non-standard (i.e. non-ISO C) functions. + \ingroup avr_stdlib +*/ /** The function itoa() converts the integer value from \c val into an ASCII representation that will be stored under \c s. The caller @@ -437,7 +459,9 @@ /address@hidden/ /address@hidden/ -/** \name Conversion functions for double arguments. */ +/** \name Conversion functions for double arguments. + \ingroup avr_stdlib +*/ /** Bit value that can be passed in \c flags to dtostre(). */ #define DTOSTR_ALWAYS_SIGN 0x01 /* put '+' or ' ' for positives */ /** Bit value that can be passed in \c flags to dtostre(). */ @@ -487,9 +511,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