rand.c revision 92986
1258299Sjmmv/*-
2258299Sjmmv * Copyright (c) 1990, 1993
3258299Sjmmv *	The Regents of the University of California.  All rights reserved.
4258299Sjmmv *
5258299Sjmmv * Redistribution and use in source and binary forms, with or without
6258299Sjmmv * modification, are permitted provided that the following conditions
7258299Sjmmv * are met:
8258299Sjmmv * 1. Redistributions of source code must retain the above copyright
9258299Sjmmv *    notice, this list of conditions and the following disclaimer.
10258299Sjmmv * 2. Redistributions in binary form must reproduce the above copyright
11258299Sjmmv *    notice, this list of conditions and the following disclaimer in the
12258299Sjmmv *    documentation and/or other materials provided with the distribution.
13258299Sjmmv * 3. All advertising materials mentioning features or use of this software
14258299Sjmmv *    must display the following acknowledgement:
15258299Sjmmv *	This product includes software developed by the University of
16258299Sjmmv *	California, Berkeley and its contributors.
17258299Sjmmv * 4. Neither the name of the University nor the names of its contributors
18258299Sjmmv *    may be used to endorse or promote products derived from this software
19258299Sjmmv *    without specific prior written permission.
20258299Sjmmv *
21258299Sjmmv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22258299Sjmmv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23258299Sjmmv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24258299Sjmmv * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25258299Sjmmv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26258299Sjmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27258299Sjmmv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28258299Sjmmv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29258299Sjmmv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30258299Sjmmv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31258299Sjmmv * SUCH DAMAGE.
32258299Sjmmv *
33258299Sjmmv * Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
34258299Sjmmv */
35258299Sjmmv
36258299Sjmmv#if defined(LIBC_SCCS) && !defined(lint)
37258299Sjmmvstatic char sccsid[] = "@(#)rand.c	8.1 (Berkeley) 6/14/93";
38258299Sjmmv#endif /* LIBC_SCCS and not lint */
39258299Sjmmv#include <sys/cdefs.h>
40258299Sjmmv__FBSDID("$FreeBSD: head/lib/libc/stdlib/rand.c 92986 2002-03-22 21:53:29Z obrien $");
41258299Sjmmv
42258299Sjmmv#include <sys/time.h>          /* for sranddev() */
43258299Sjmmv#include <sys/types.h>
44258299Sjmmv#include <fcntl.h>             /* for sranddev() */
45258299Sjmmv#include <stdlib.h>
46258299Sjmmv#include <unistd.h>            /* for sranddev() */
47258299Sjmmv#include "un-namespace.h"
48258299Sjmmv
49258299Sjmmv#ifdef TEST
50258299Sjmmv#include <stdio.h>
51258299Sjmmv#endif /* TEST */
52258299Sjmmv
53258299Sjmmvstatic int
54258299Sjmmvdo_rand(unsigned long *ctx)
55258299Sjmmv{
56258299Sjmmv#ifdef  USE_WEAK_SEEDING
57258299Sjmmv/*
58258299Sjmmv * Historic implementation compatibility.
59258299Sjmmv * The random sequences do not vary much with the seed,
60258299Sjmmv * even with overflowing.
61258299Sjmmv */
62258299Sjmmv	return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
63258299Sjmmv#else   /* !USE_WEAK_SEEDING */
64258299Sjmmv/*
65258299Sjmmv * Compute x = (7^5 * x) mod (2^31 - 1)
66258299Sjmmv * wihout overflowing 31 bits:
67258299Sjmmv *      (2^31 - 1) = 127773 * (7^5) + 2836
68258299Sjmmv * From "Random number generators: good ones are hard to find",
69258299Sjmmv * Park and Miller, Communications of the ACM, vol. 31, no. 10,
70258299Sjmmv * October 1988, p. 1195.
71258299Sjmmv */
72258299Sjmmv	long hi, lo, x;
73258299Sjmmv
74258299Sjmmv	hi = *ctx / 127773;
75258299Sjmmv	lo = *ctx % 127773;
76258299Sjmmv	x = 16807 * lo - 2836 * hi;
77258299Sjmmv	if (x <= 0)
78258299Sjmmv		x += 0x7fffffff;
79258299Sjmmv	return ((*ctx = x) % ((u_long)RAND_MAX + 1));
80258299Sjmmv#endif  /* !USE_WEAK_SEEDING */
81258299Sjmmv}
82258299Sjmmv
83258299Sjmmv
84258299Sjmmvint
85258299Sjmmvrand_r(unsigned int *ctx)
86258299Sjmmv{
87258299Sjmmv	u_long val = (u_long) *ctx;
88258299Sjmmv	*ctx = do_rand(&val);
89258299Sjmmv	return (int) *ctx;
90258299Sjmmv}
91258299Sjmmv
92258299Sjmmv
93258299Sjmmvstatic u_long next = 1;
94258299Sjmmv
95258299Sjmmvint
96258299Sjmmvrand()
97258299Sjmmv{
98258299Sjmmv	return do_rand(&next);
99258299Sjmmv}
100258299Sjmmv
101258299Sjmmvvoid
102258299Sjmmvsrand(seed)
103258299Sjmmvu_int seed;
104258299Sjmmv{
105258299Sjmmv	next = seed;
106258299Sjmmv}
107258299Sjmmv
108258299Sjmmv
109258299Sjmmv/*
110258299Sjmmv * sranddev:
111258299Sjmmv *
112258299Sjmmv * Many programs choose the seed value in a totally predictable manner.
113258299Sjmmv * This often causes problems.  We seed the generator using the much more
114258299Sjmmv * secure random(4) interface.
115258299Sjmmv */
116258299Sjmmvvoid
117258299Sjmmvsranddev()
118258299Sjmmv{
119258299Sjmmv	int fd, done;
120
121	done = 0;
122	fd = _open("/dev/random", O_RDONLY, 0);
123	if (fd >= 0) {
124		if (_read(fd, (void *) &next, sizeof(next)) == sizeof(next))
125			done = 1;
126		_close(fd);
127	}
128
129	if (!done) {
130		struct timeval tv;
131		unsigned long junk;
132
133		gettimeofday(&tv, NULL);
134		next = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk;
135	}
136}
137
138
139#ifdef TEST
140
141main()
142{
143    int i;
144    unsigned myseed;
145
146    printf("seeding rand with 0x19610910: \n");
147    srand(0x19610910);
148
149    printf("generating three pseudo-random numbers:\n");
150    for (i = 0; i < 3; i++)
151    {
152	printf("next random number = %d\n", rand());
153    }
154
155    printf("generating the same sequence with rand_r:\n");
156    myseed = 0x19610910;
157    for (i = 0; i < 3; i++)
158    {
159	printf("next random number = %d\n", rand_r(&myseed));
160    }
161
162    return 0;
163}
164
165#endif /* TEST */
166
167