xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/myrand.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1 /*	$NetBSD: myrand.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	myrand 3
6 /* SUMMARY
7 /*	rand wrapper
8 /* SYNOPSIS
9 /*	#include <myrand.h>
10 /*
11 /*	void	mysrand(seed)
12 /*	int	seed;
13 /*
14 /*	int	myrand()
15 /* DESCRIPTION
16 /*	This module implements a wrapper for the portable, pseudo-random
17 /*	number generator. The wrapper adds automatic initialization.
18 /*
19 /*	mysrand() performs initialization. This call may be skipped.
20 /*
21 /*	myrand() returns a pseudo-random number in the range [0, RAND_MAX].
22 /*	If mysrand() was not called, it is invoked with the process ID
23 /*	ex-or-ed with the time of day in seconds.
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /*	The Secure Mailer license must be distributed with this software.
28 /* WARNING
29 /*	Do not use this code for generating unpredictable numbers.
30 /* AUTHOR(S)
31 /*	Wietse Venema
32 /*	IBM T.J. Watson Research
33 /*	P.O. Box 704
34 /*	Yorktown Heights, NY 10598, USA
35 /*--*/
36 
37 /* System library. */
38 
39 #include <sys_defs.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <time.h>
43 
44 /* Utility library. */
45 
46 #include <myrand.h>
47 
48 static int myrand_initdone = 0;
49 
50 /* mysrand - initialize */
51 
mysrand(int seed)52 void    mysrand(int seed)
53 {
54     srand(seed);
55     myrand_initdone = 1;
56 }
57 
58 /* myrand - pseudo-random number */
59 
myrand(void)60 int     myrand(void)
61 {
62     if (myrand_initdone == 0)
63 	mysrand(getpid() ^ time((time_t *) 0));
64     return (rand());
65 }
66