blob: 6ee17a0a3af1f13f5022388b5ca38a58c1883aba [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*********************************************************************/
2/* */
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 */
7/* */
8/*********************************************************************/
9
10#include <sys/types.h>
11#include <sys/times.h>
12
hasso29e50b22005-09-01 18:18:47 +000013unsigned long timer()
jardineb5d44e2003-12-23 08:09:43 +000014 { struct tms hold;
15
16 times(&hold);
hasso29e50b22005-09-01 18:18:47 +000017 return (unsigned long) ((float) (hold.tms_utime) / 60.0);
jardineb5d44e2003-12-23 08:09:43 +000018 }
19
20
21/*********************************************************************/
22/* */
23/* Family of random number generators */
24/* */
25/* Initialisation: */
26/* void init_rand ( seed ); */
27/* long seed - any positive number */
28/* if seed<=0 init_rand takes time */
29/* from timer instead of seed */
30/* */
31/* Whole number uniformly distributed on [0,n): */
32/* long nrand (n); */
33/* long n */
34/* */
35/* Real number uniformly distributed on [0,1] */
36/* double rand01(); */
37/* */
38/* Real number with Gauss(0,1) disitribution: */
39/* double randg01(); */
40/* */
41/* Algorithm: */
42/* x(n+1) = (x(n) * 5^13) mod 2^31 */
43/* */
44/*********************************************************************/
45
46unsigned long internal_seed;
47
48void init_rand ( init_seed )
49
50long init_seed;
51
52{ internal_seed = ( init_seed > 0 )
53 ? (unsigned long) init_seed
54 : (unsigned long) timer();
55
56
57 /* only odd numbers are acceptable */
58 if ( internal_seed % 2 == 0 ) internal_seed --;
59}
60
61/*********************************************************************/
62/* */
63/* Internal function irand may depend on OS and compiler */
64/* */
65/* irand assumption: */
66/* unsigned long i,j; */
67/* if i*j > max(unsigned long) */
68/* 1. No overflow interruption */
69/* 2. i*j = i*j mod max(unsigned long) */
70/* */
71/* This assumption is true for a lot of computers. */
72/* If your computer fails: */
73/* rename: irand <---> xrand */
74/* */
75/*********************************************************************/
76
77#define A 1220703125
78#define B 2147483647
79#define BF 2147483647.
80
81static long irand ()
82
83{ internal_seed = ( internal_seed * A ) & B;
84 return (long) internal_seed ;
85}
86
hasso29e50b22005-09-01 18:18:47 +000087#if 0 /* Not used. */
jardineb5d44e2003-12-23 08:09:43 +000088/*********************************************************************/
89/* */
90/* computer independent variant of irand */
91/* */
92/*********************************************************************/
93
94
95#define T15 32768
96#define T16 65536
97#define A1 37252
98#define A2 29589
99
100static long xrand()
101
102{ unsigned long is1, is2;
103
104 is1 = internal_seed / T15;
105 is2 = internal_seed % T15;
106
107 internal_seed = ( (((is2 * A1) + (is1 * A2))% T16 )* T15 + (is2 * A2) ) & B;
108 return (long) ( internal_seed ) ;
109}
hasso29e50b22005-09-01 18:18:47 +0000110#endif
jardineb5d44e2003-12-23 08:09:43 +0000111
112/*********************************************************************/
113
114
115double rand01()
116
hasso29e50b22005-09-01 18:18:47 +0000117{ return (double) (irand() / BF) ;
jardineb5d44e2003-12-23 08:09:43 +0000118}
119
120/*********************************************************************/
121
122#define NK 12
123
124double randg01()
125
126{ int i;
127 double sum = 0;
128
129 for ( i = 0; i < NK; i++ ) sum += rand01();
130 return sum - 6.;
131
132 /* if NK != 12 then you must return (12/NK)*sum - (NK/2) */
133}
134
135#undef NK
136
137
138/*********************************************************************/
139
140long nrand ( n )
141
142long n;
143
144{ return (long) ( rand01() * (double) n );
145}
146
147/*********************************************************************/
148
149#undef A
150#undef A1
151#undef A2
152#undef B
153#undef BF
154#undef T15
155#undef T16