1 /*
2  *   Copyright (C) 1989-1991 Yale University
3  *
4  *   This work is distributed in the hope that it will be useful; you can
5  *   redistribute it and/or modify it under the terms of the
6  *   GNU General Public License as published by the Free Software Foundation;
7  *   either version 2 of the License,
8  *   or any later version, on the following conditions:
9  *
10  *   (a) YALE MAKES NO, AND EXPRESSLY DISCLAIMS
11  *   ALL, REPRESENTATIONS OR WARRANTIES THAT THE MANUFACTURE, USE, PRACTICE,
12  *   SALE OR
13  *   OTHER DISPOSAL OF THE SOFTWARE DOES NOT OR WILL NOT INFRINGE UPON ANY
14  *   PATENT OR
15  *   OTHER RIGHTS NOT VESTED IN YALE.
16  *
17  *   (b) YALE MAKES NO, AND EXPRESSLY DISCLAIMS ALL, REPRESENTATIONS AND
18  *   WARRANTIES
19  *   WHATSOEVER WITH RESPECT TO THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
20  *   INCLUDING,
21  *   BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
22  *   PARTICULAR
23  *   PURPOSE.
24  *
25  *   (c) LICENSEE SHALL MAKE NO STATEMENTS, REPRESENTATION OR WARRANTIES
26  *   WHATSOEVER TO
27  *   ANY THIRD PARTIES THAT ARE INCONSISTENT WITH THE DISCLAIMERS BY YALE IN
28  *   ARTICLE
29  *   (a) AND (b) above.
30  *
31  *   (d) IN NO EVENT SHALL YALE, OR ITS TRUSTEES, DIRECTORS, OFFICERS,
32  *   EMPLOYEES AND
33  *   AFFILIATES BE LIABLE FOR DAMAGES OF ANY KIND, INCLUDING ECONOMIC DAMAGE OR
34  *   INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER YALE SHALL BE
35  *   ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE
36  *   POSSIBILITY OF THE FOREGOING.
37  *
38  */
39 
40 /* --------------------------------------------------------------------------
41 FILE:	    mytime.c
42 DESCRIPTION:better random seed from system time.
43 CONTENTS:   unsigned Yrandom_seed()
44 DATE:	    Feb 07, 1989
45 REVISIONS:  Apr 27, 1989 - changed to Y prefix.
46 	    Aug 25, 1989 - changed algorithm to use less system
47 		calls.
48 	    Dec 17, 1989 - use the version in SC as default.
49 	    Apr 01, 1991 - added SYS5 (A/UX) support  (RAWeier)
50             Oct 07, 1991 - fix warning in Gnu gcc (RAWeier)
51             Dec 09, 1991 - industrial users say gettimeofday is more
52                            "universal" and improves portability (RAWeier)
53 --------------------------------------------------------------------------- */
54 
55 
56 #include <yalecad/base.h>
57 
58 /* NOTE to user on foreign machines: if below does not compile, you */
59 /* may use the alternate random seed generator at some penalty for */
60 /* randomness but it should not matter due to the robustness of the */
61 /* random number generator itself.  Just use #define ALTERNATE */
62 
63 /* #define ALTERNATE */
64 
65 #ifndef ALTERNATE
66 
67 #ifdef VMS
68 #include <types.h>
69 #include <timeb.h>
70 
71 #else
72 #include <sys/types.h>
73 #include <sys/time.h>
74 #endif
75 
Yrandom_seed()76 unsigned Yrandom_seed()
77 {
78     UNSIGNED_INT m , time1 , time2 ;
79     struct timeval tp ;
80     gettimeofday(&tp,0) ;
81     time1 = (UNSIGNED_INT) tp.tv_usec ;
82     time1 <<= 22 ;
83     time2 = (tp.tv_usec & 0x003FFFFF) ;
84     m = time1 | time2 ;
85     return(m>>1);
86 }
87 
88 #else  /* ALTERNATE */
89 
90 /* returns a random seed from system clock */
Yrandom_seed()91 unsigned Yrandom_seed()
92 {
93     UNSIGNED_INT seed ;
94     long t ;
95     INT  hsum ;
96     INT  shift ;
97     char buffer[LRECL],
98 	 *name = buffer ;;
99 
100     /* get time since Jan 1, 1970 in seconds */
101     t = time((long *) 0) ;
102     /* convert to a string */
103     sprintf( buffer, "%d", t ) ;
104 
105     /* now use hash function to get a number 0..INT_MAX */
106     /* don't initialize hsum use garbage if possible */
107     for (shift=1 ;*name; name++){
108 	hsum = hsum + *name<<shift;
109 	shift = (shift + 1) & 0x000F;
110     }
111     return((unsigned) (hsum % 0x003FFFFF) ) ;
112 
113 } /* end my_time */
114 
115 #endif /* ALTERNATE */
116 
117 
118 
119 
120 
121