source: trunk/indexers/mg/lib/random.c@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/**************************************************************************
2 *
3 * random.c -- pseudo random number generator
4 * Copyright (C) 1994 Chris Wallace ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: random.c 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************/
23
24/*
25$Log$
26Revision 1.1 2003/02/20 21:14:16 mdewsnip
27Addition of MG package for search and retrieval
28
29Revision 1.1 1999/08/10 21:16:57 sjboddie
30renamed mg-1.3d directory mg
31
32Revision 1.1 1998/11/17 09:32:20 rjmcnab
33*** empty log message ***
34
35*/
36
37static char *RCSID = "$Id: random.c 3745 2003-02-20 21:20:24Z mdewsnip $";
38
39/*
40 * A random number generator called as a function by
41 * random (iseed) or irandm (iseed)
42 * The parameter should be a pointer to a 2-element long vector.
43 * The first function returns a double uniform in 0 .. 1.
44 * The second returns a long integer uniform in 0 .. 2**31-1
45 * Both update iseed[] in exactly the same way.
46 * iseed[] must be a 2-element integer vector.
47 * The initial value of the second element may be anything.
48 *
49 * The period of the random sequence is 2**32 * (2**32-1)
50 * The table mt[0:127] is defined by mt[i] = 69069 ** (128-i)
51 */
52
53#define MASK ((long) 593970775)
54/* or in hex, 23674657 */
55
56#define SCALE ((double) 1.0 / (1024.0 * 1024.0 * 1024.0 * 2.0))
57/* i.e. 2 to power -31 */
58
59static long mt [128] = {
60 902906369,
61 2030498053,
62 -473499623,
63 1640834941,
64 723406961,
65 1993558325,
66 -257162999,
67 -1627724755,
68 913952737,
69 278845029,
70 1327502073,
71 -1261253155,
72 981676113,
73 -1785280363,
74 1700077033,
75 366908557,
76 -1514479167,
77 -682799163,
78 141955545,
79 -830150595,
80 317871153,
81 1542036469,
82 -946413879,
83 -1950779155,
84 985397153,
85 626515237,
86 530871481,
87 783087261,
88 -1512358895,
89 1031357269,
90 -2007710807,
91 -1652747955,
92 -1867214463,
93 928251525,
94 1243003801,
95 -2132510467,
96 1874683889,
97 -717013323,
98 218254473,
99 -1628774995,
100 -2064896159,
101 69678053,
102 281568889,
103 -2104168611,
104 -165128239,
105 1536495125,
106 -39650967,
107 546594317,
108 -725987007,
109 1392966981,
110 1044706649,
111 687331773,
112 -2051306575,
113 1544302965,
114 -758494647,
115 -1243934099,
116 -75073759,
117 293132965,
118 -1935153095,
119 118929437,
120 807830417,
121 -1416222507,
122 -1550074071,
123 -84903219,
124 1355292929,
125 -380482555,
126 -1818444007,
127 -204797315,
128 170442609,
129 -1636797387,
130 868931593,
131 -623503571,
132 1711722209,
133 381210981,
134 -161547783,
135 -272740131,
136 -1450066095,
137 2116588437,
138 1100682473,
139 358442893,
140 -1529216831,
141 2116152005,
142 -776333095,
143 1265240893,
144 -482278607,
145 1067190005,
146 333444553,
147 86502381,
148 753481377,
149 39000101,
150 1779014585,
151 219658653,
152 -920253679,
153 2029538901,
154 1207761577,
155 -1515772851,
156 -236195711,
157 442620293,
158 423166617,
159 -1763648515,
160 -398436623,
161 -1749358155,
162 -538598519,
163 -652439379,
164 430550625,
165 -1481396507,
166 2093206905,
167 -1934691747,
168 -962631983,
169 1454463253,
170 -1877118871,
171 -291917555,
172 -1711673279,
173 201201733,
174 -474645415,
175 -96764739,
176 -1587365199,
177 1945705589,
178 1303896393,
179 1744831853,
180 381957665,
181 2135332261,
182 -55996615,
183 -1190135011,
184 1790562961,
185 -1493191723,
186 475559465,
187 69069
188 };
189
190double
191random (long is [2])
192{
193 long it, leh, nit;
194
195 it = is [0];
196 leh = is [1];
197 if (it <= 0)
198 it = (it + it) ^ MASK;
199 else
200 it = it + it;
201 nit = it - 1;
202/* to ensure all-ones pattern omitted */
203 leh = leh * mt[nit & 127] + nit;
204 is [0] = it; is [1] = leh;
205 if (leh < 0) leh = ~leh;
206 return (SCALE * ((long) (leh | 1)));
207}
208
209
210
211long
212irandm (long is [2])
213{
214 long it, leh, nit;
215
216 it = is [0];
217 leh = is [1];
218 if (it <= 0)
219 it = (it + it) ^ MASK;
220 else
221 it = it + it;
222 nit = it - 1;
223/* to ensure all-ones pattern omitted */
224 leh = leh * mt[nit & 127] + nit;
225 is [0] = it; is [1] = leh;
226 if (leh < 0) leh = ~leh;
227 return (leh);
228}
Note: See TracBrowser for help on using the repository browser.