1 /*********************************************************************/
3 /* current processor time in seconds */
4 /* difference between two calls is processor time spent by your code */
5 /* needs: <sys/types.h>, <sys/times.h> */
6 /* depends on compiler and OS */
8 /*********************************************************************/
11 #include <sys/types.h>
12 #include <sys/times.h>
17 unsigned long timer(void);
22 void free_arc(void *);
24 unsigned long timer ()
28 return (unsigned long) ((float) (hold.tms_utime) / 60.0);
32 /*********************************************************************/
34 /* Family of random number generators */
37 /* void init_rand ( seed ); */
38 /* long seed - any positive number */
39 /* if seed<=0 init_rand takes time */
40 /* from timer instead of seed */
42 /* Whole number uniformly distributed on [0,n): */
46 /* Real number uniformly distributed on [0,1] */
47 /* double rand01(); */
49 /* Real number with Gauss(0,1) disitribution: */
50 /* double randg01(); */
53 /* x(n+1) = (x(n) * 5^13) mod 2^31 */
55 /*********************************************************************/
57 unsigned long internal_seed;
59 void init_rand ( init_seed )
63 { internal_seed = ( init_seed > 0 )
64 ? (unsigned long) init_seed
65 : (unsigned long) timer();
68 /* only odd numbers are acceptable */
69 if ( internal_seed % 2 == 0 ) internal_seed --;
72 /*********************************************************************/
74 /* Internal function irand may depend on OS and compiler */
76 /* irand assumption: */
77 /* unsigned long i,j; */
78 /* if i*j > max(unsigned long) */
79 /* 1. No overflow interruption */
80 /* 2. i*j = i*j mod max(unsigned long) */
82 /* This assumption is true for a lot of computers. */
83 /* If your computer fails: */
84 /* rename: irand <---> xrand */
86 /*********************************************************************/
90 #define BF 2147483647.
94 { internal_seed = ( internal_seed * A ) & B;
95 return (long) internal_seed ;
99 /*********************************************************************/
101 /* computer independent variant of irand */
103 /*********************************************************************/
113 { unsigned long is1, is2;
115 is1 = internal_seed / T15;
116 is2 = internal_seed % T15;
118 internal_seed = ( (((is2 * A1) + (is1 * A2))% T16 )* T15 + (is2 * A2) ) & B;
119 return (long) ( internal_seed ) ;
123 /*********************************************************************/
128 { return (double) (irand() / BF) ;
131 /*********************************************************************/
140 for ( i = 0; i < NK; i++ ) sum += rand01();
143 /* if NK != 12 then you must return (12/NK)*sum - (NK/2) */
149 /*********************************************************************/
155 { return (long) ( rand01() * (double) n );
158 /*********************************************************************/