10
10
* 2. Redistributions in binary form must reproduce the above copyright
11
11
* notice, this list of conditions and the following disclaimer in the
12
12
* documentation and/or other materials provided with the distribution.
13
* 3. All advertising materials mentioning features or use of this software
14
* must display the following acknowledgement:
15
* This product includes software developed by the University of
16
* California, Berkeley and its contributors.
17
* 4. Neither the name of the University nor the names of its contributors
13
* 3. Neither the name of the University nor the names of its contributors
18
14
* may be used to endorse or promote products derived from this software
19
15
* without specific prior written permission.
33
29
* Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
31
* $Id: rand.c,v 1.2 2002/10/16 15:52:25 joerg_wunsch Exp $
36
#if defined(LIBC_SCCS) && !defined(lint)
37
36
static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93";
38
#endif /* LIBC_SCCS and not lint */
41
#include <sys/types.h>
43
39
#include <stdlib.h>
50
42
do_rand(unsigned long *ctx)
52
return ((*ctx = *ctx * 1103515245 + 12345) % ((unsigned long int)RAND_MAX + 1));
44
#ifdef USE_WEAK_SEEDING
46
* Historic implementation compatibility.
47
* The random sequences do not vary much with the seed,
48
* even with overflowing.
50
return ((*ctx = *ctx * 1103515245L + 12345L) %
51
((unsigned long)RAND_MAX + 1));
52
#else /* !USE_WEAK_SEEDING */
54
* Compute x = (7^5 * x) mod (2^31 - 1)
55
* wihout overflowing 31 bits:
56
* (2^31 - 1) = 127773 * (7^5) + 2836
57
* From "Random number generators: good ones are hard to find",
58
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
59
* October 1988, p. 1195.
65
x = 16807L * lo - 2836L * hi;
68
return ((*ctx = x) % ((unsigned long)RAND_MAX + 1));
69
#endif /* !USE_WEAK_SEEDING */
57
rand_r(unsigned int *ctx)
74
rand_r(unsigned long *ctx)
59
unsigned long int val = (unsigned long int) *ctx;
65
static unsigned long int next = 1;
80
static unsigned long next = 1;
86
printf("seeding rand with 0x19610910: \n");
89
printf("generating three pseudo-random numbers:\n");
90
for (i = 0; i < 3; i++)
92
printf("next random number = %d\n", rand());
95
printf("generating the same sequence with rand_r:\n");
97
for (i = 0; i < 3; i++)
99
printf("next random number = %d\n", rand_r(&myseed));