source: trunk/gsdl/src/mgpp/lib/random.cpp@ 855

Last change on this file since 855 was 855, checked in by sjboddie, 24 years ago

Rodgers new C++ mg

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