1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* file random.c - generator
*
* $Id: random.c,v 1.8 2006/02/10 15:07:41 fzago Exp $
*
* Program XBLAST
* (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2; or (at your option)
* any later version
*
* This program is distributed in the hope that it will be entertaining,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "xblast.h"
/*
* local Variables
*/
/* seeds for both random generators */
static unsigned gRand;
static unsigned oRand;
/*
This Random Number Generator is based on the algorithm in a FORTRAN
version published by George Marsaglia and Arif Zaman, Florida State
University; ref.: see original comments below.
At the fhw (Fachhochschule Wiesbaden, W.Germany), Dept. of Computer
Science, we have written sources in further languages (C, Modula-2
Turbo-Pascal(3.0, 5.0), Basic and Ada) to get exactly the same test
results compared with the original FORTRAN version.
April 1989
Karl-L. Noell <NOELL@DWIFH1.BITNET>
and Helmut Weber <WEBER@DWIFH1.BITNET>
This random number generator originally appeared in "Toward a Universal
Random Number Generator" by George Marsaglia and Arif Zaman.
Florida State University Report: FSU-SCRI-87-50 (1987)
It was later modified by F. James and published in "A Review of Pseudo-
random Number Generators"
THIS IS THE BEST KNOWN RANDOM NUMBER GENERATOR AVAILABLE.
(However, a newly discovered technique can yield
a period of 10^600. But that is still in the development stage.)
It passes ALL of the tests for random number generators and has a period
of 2^144, is completely portable (gives bit identical results on all
machines with at least 24-bit mantissas in the floating point
representation).
The algorithm is a combination of a Fibonacci sequence (with lags of 97
and 33, and operation "subtraction plus one, modulo one") and an
"arithmetic sequence" (using subtraction).
Use IJ = 1802 & KL = 9373 to test the random number generator. The
subroutine RANMAR should be used to generate 20000 random numbers.
Then display the next six random numbers generated multiplied by 4096*4096
If the random number generator is working properly, the random numbers
should be:
6533892.0 14220222.0 7275067.0
6172232.0 8354498.0 10633180.0
*/
/* Globals */
double u[97], c, cd, cm;
int i97, j97;
int test = 0;
/*
This is the initialization routine for the random number generator.
NOTE: The seed variables can have values between: 0 <= IJ <= 31328
0 <= KL <= 30081
The random number sequences created by these two seeds are of sufficient
length to complete an entire calculation with. For example, if sveral
different groups are working on different parts of the same calculation,
each group could be assigned its own IJ seed. This would leave each group
with 30000 choices for the second seed. That is to say, this random
number generator can create 900 million different subsequences -- with
each subsequence having a length of approximately 10^30.
*/
static void
RandomInitialise (int ij, int kl)
{
double s, t;
int ii, i, j, k, l, jj, m;
/*
Handle the seed range errors
First random number seed must be between 0 and 31328
Second seed must have a value between 0 and 30081
*/
if (ij < 0 || ij > 31328) {
ij = 1802;
}
if (kl < 0 || kl > 30081) {
kl = 9373;
}
i = (ij / 177) % 177 + 2;
j = (ij % 177) + 2;
k = (kl / 169) % 178 + 1;
l = (kl % 169);
for (ii = 0; ii < 97; ii++) {
s = 0.0;
t = 0.5;
for (jj = 0; jj < 24; jj++) {
m = (((i * j) % 179) * k) % 179;
i = j;
j = k;
k = m;
l = (53 * l + 1) % 169;
if (((l * m % 64)) >= 32)
s += t;
t *= 0.5;
}
u[ii] = s;
}
c = 362436.0 / 16777216.0;
cd = 7654321.0 / 16777216.0;
cm = 16777213.0 / 16777216.0;
i97 = 97;
j97 = 33;
test = 1;
} /* RandomInitialize */
/*
This is the random number generator proposed by George Marsaglia in
Florida State University Report: FSU-SCRI-87-50
*/
static double
RandomUniform (void)
{
double uni;
/* Make sure the initialisation routine has been called */
if (!test)
RandomInitialise (1802, 9373);
uni = u[i97 - 1] - u[j97 - 1];
if (uni <= 0.0)
uni++;
u[i97 - 1] = uni;
i97--;
if (i97 == 0)
i97 = 97;
j97--;
if (j97 == 0)
j97 = 97;
c -= cd;
if (c < 0.0)
c += cm;
uni -= c;
if (uni < 0.0)
uni++;
return (uni);
} /* RandomUniform */
#ifdef unused
/*
ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM.
THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435.
The function returns a normally distributed pseudo-random number
with a given mean and standard devaiation. Calls are made to a
function subprogram which must return independent random
numbers uniform in the interval (0,1).
The algorithm uses the ratio of uniforms method of A.J. Kinderman
and J.F. Monahan augmented with quadratic bounding curves.
*/
static double
RandomGaussian (double mean, double stddev)
{
double q, u, v, x, y;
/*
Generate P = (u,v) uniform in rect. enclosing acceptance region
Make sure that any random numbers <= 0 are rejected, since
gaussian() requires uniforms > 0, but RandomUniform() delivers >= 0.
*/
do {
u = RandomUniform ();
v = RandomUniform ();
if (u <= 0.0 || v <= 0.0) {
u = 1.0;
v = 1.0;
}
v = 1.7156 * (v - 0.5);
/* Evaluate the quadratic form */
x = u - 0.449871;
y = fabs (v) + 0.386595;
q = x * x + y * (0.19600 * y - 0.25472 * x);
/* Accept P if inside inner ellipse */
if (q < 0.27597)
break;
/* Reject P if outside outer ellipse, or outside acceptance region */
} while ((q > 0.27846) || (v * v > -4.0 * log (u) * u * u));
/* Return ratio of P's coordinates as the normal deviate */
return (mean + stddev * v / u);
} /* RandomGaussian */
#endif
/*
Return random integer within a range, lower -> upper INCLUSIVE
*/
static int
RandomInt (int lower, int upper)
{
return ((int)(RandomUniform () * (upper - lower + 1)) + lower);
} /* RandomInt */
#ifdef unused
/*
Return random float within a range, lower -> upper
*/
static double
RandomDouble (double lower, double upper)
{
return ((upper - lower) * RandomUniform () + lower);
} /* RandomDouble */
#endif
/*
* seeds both random number generators
*/
void
SeedRandom (unsigned seed)
{
int i;
gRand = oRand = seed;
RandomInitialise (seed % 31328, (seed / 313) % 30081);
for (i = 0; i < 100; i++) {
GameRandomNumber (100);
}
} /* SeedRandom */
/*
* get the seed of the random number generator
*/
unsigned
GetRandomSeed (void)
{
return gRand;
} /* GetRandomSeed */
/*
* creates a 32 Bit pseudo random number
*/
static unsigned
GameRandom (void)
{
gRand = 1664525L * gRand + 1013904223L;
return gRand;
} /* GameRandom */
/*
* creates a 32 Bit pseudo random number
*/
/*
static unsigned
OtherRandom (void)
{
oRand = 1664525L * oRand + 1013904223L;
return oRand;
}
*/
/* MyRandom */
/*
* creates an integer random number between 0 and maxVal-1;
*/
int
GameRandomNumber1 (int maxVal)
{
/* return (int)(GameRandom () % maxVal); */
/* fprintf(stderr,"new gamerandom\n"); */
return RandomInt (0, maxVal - 1);
} /* RandomNumber */
/*
* creates an integer random number between 0 and maxVal-1;
*/
int
GameRandomNumber2 (int maxVal, const char *file, int line)
{
int result = (int)(GameRandom () % maxVal);
fprintf (stderr, "%s:%d: GameRandomNumber() = %d/%d (%u)\n", file, line, result, maxVal,
gRand);
return result;
} /* RandomNumber */
/*
* creates an integer random number between 0 and maxVal-1;
*/
int
OtherRandomNumber (int maxVal)
{
/* return (int) (OtherRandom () % maxVal); */
/* printf("new otherrandom\n"); */
return RandomInt (0, maxVal - 1);
} /* RandomNumber */
/*
* end of file random.c
*/
|