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