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 /*********************************************************************/
10 #include <sys/types.h>
11 #include <sys/times.h>
16 unsigned long timer(void);
21 void free_arc(void *);
23 unsigned long timer ()
27 return (unsigned long) ((float) (hold
.tms_utime
) / 60.0);
31 /*********************************************************************/
33 /* Family of random number generators */
36 /* void init_rand ( seed ); */
37 /* long seed - any positive number */
38 /* if seed<=0 init_rand takes time */
39 /* from timer instead of seed */
41 /* Whole number uniformly distributed on [0,n): */
45 /* Real number uniformly distributed on [0,1] */
46 /* double rand01(); */
48 /* Real number with Gauss(0,1) disitribution: */
49 /* double randg01(); */
52 /* x(n+1) = (x(n) * 5^13) mod 2^31 */
54 /*********************************************************************/
56 unsigned long internal_seed
;
58 void init_rand ( init_seed
)
62 { internal_seed
= ( init_seed
> 0 )
63 ? (unsigned long) init_seed
64 : (unsigned long) timer();
67 /* only odd numbers are acceptable */
68 if ( internal_seed
% 2 == 0 ) internal_seed
--;
71 /*********************************************************************/
73 /* Internal function irand may depend on OS and compiler */
75 /* irand assumption: */
76 /* unsigned long i,j; */
77 /* if i*j > max(unsigned long) */
78 /* 1. No overflow interruption */
79 /* 2. i*j = i*j mod max(unsigned long) */
81 /* This assumption is true for a lot of computers. */
82 /* If your computer fails: */
83 /* rename: irand <---> xrand */
85 /*********************************************************************/
89 #define BF 2147483647.
93 { internal_seed
= ( internal_seed
* A
) & B
;
94 return (long) internal_seed
;
98 /*********************************************************************/
100 /* computer independent variant of irand */
102 /*********************************************************************/
112 { unsigned long is1
, is2
;
114 is1
= internal_seed
/ T15
;
115 is2
= internal_seed
% T15
;
117 internal_seed
= ( (((is2
* A1
) + (is1
* A2
))% T16
)* T15
+ (is2
* A2
) ) & B
;
118 return (long) ( internal_seed
) ;
122 /*********************************************************************/
127 { return (double) (irand() / BF
) ;
130 /*********************************************************************/
139 for ( i
= 0; i
< NK
; i
++ ) sum
+= rand01();
142 /* if NK != 12 then you must return (12/NK)*sum - (NK/2) */
148 /*********************************************************************/
154 { return (long) ( rand01() * (double) n
);
157 /*********************************************************************/