I'm trying to understand the concept of Random Number Generator and now I have trouble understanding this piece of C++ code in the book that I am studying right now.
static double drng[][6] =
{
0, 0, 1, 0, 0, 1,
1772212344, 1374954571, 2377447708, 540628578, 1843308759, 549575061,
2602294560, 1764491502, 3872775590, 4089362440, 2683806282, 437563332,
376810349, 1545165407, 3443838735, 3650079346, 1898051052, 2606578666,
1847817841, 3038743716, 2014183350, 2883836363, 3242147124, 1955620878,
1075987441, 3468627582, 2694529948, 368150488, 2026479331, 2067041056,
134547324, 4246812979, 1700384422, 2358888058, 83616724, 3045736624,
2816844169, 885735878, 1824365395, 2629582008, 3405363962, 1835381773,
675808621, 434584068, 4021752986, 3831444678, 4193349505, 2833414845,
2876117643, 1466108979, 163986545, 1530526354, 68578399, 1111539974,
411040508, 544377427, 2887694751, 702892456, 758163486, 2462939166,
3631741414, 3388407961, 1205439229, 581001230, 3728119407, 94602786,
4267066799, 3221182590, 2432930550, 813784585, 1980232156, 2376040999,
1601564418, 2988901653, 4114588926, 2447029331, 4071707675, 3696447685,
3878417653, 2549122180, 1351098226, 3888036970, 1344540382, 2430069028,
197118588, 1885407936, 576504243, 439732583, 103559440, 3361573194,
4024454184, 2530169746, 2135879297, 2516366026, 260078159, 2905856966,
2331743881, 2059737664, 186644977, 401315249, 72328980, 1082588425,
694808921, 2851138195, 1756125381, 1738505503, 2662188364, 3598740668,
2834735415, 2017577369, 3257393066, 3823680297, 2315410613, 637316697,
4132025555, 3700940887, 838767760, 2818574268, 1375004287, 2172829019,
...
560024289, 1830276631, 144885590, 1556615741, 1597610225, 1856413969,
1031792556, 1844191084, 1441357589, 3147919604, 199001354, 2555043119,
2023049680, 4184669824, 4074523931, 252765086, 3328098427, 1480103038
};
#define norm 2.328306549295728e-10 /* 1.0/(m1+1) */
#define norm2 2.328318825240738e-10 /* 1.0/(m2+1) */
#define m1 4294967087.0
#define m2 4294944443.0
double mrand(int stream)
{
long k;
double p,
s10 = drng[stream][0], s11 = drng[stream][1], s12 = drng[stream][2],
s20 = drng[stream][3], s21 = drng[stream][4], s22 = drng[stream][5];
p = 1403580.0 * s11 - 810728.0 * s10;
k = p / m1; p -= k*m1; if (p < 0.0) p += m1;
s10 = s11; s11 = s12; s12 = p;
p = 527612.0 * s22 - 1370589.0 * s20;
k = p / m2; p -= k*m2; if (p < 0.0) p += m2;
s20 = s21; s21 = s22; s22 = p;
drng[stream][0] = s10; drng[stream][1] = s11; drng[stream][2] = s12;
drng[stream][3] = s20; drng[stream][4] = s21; drng[stream][5] = s22;
if (s12 <= s22) return ((s12 - s22 + m1) * norm);
else return ((s12 - s22) * norm);
}
/* Set seed vector for stream "stream". */
void mrandst(double* seed, int stream)
{
int i;
for (i = 0; i <= 5; ++i) drng[stream][i] = seed[i];
}
/* Get seed vector for stream "stream". */
void mrandgt(double* seed, int stream)
{
int i;
for (i = 0; i <= 5; ++i) seed[i] = drng[stream][i];
}
I know that the drng
list is the seeds to Random Generators but I can't understand that how this code is working and also what is the role of the mrandgt
and mrandst
functions in this code and when should we use them?
Thanks in advance for answering my question.
Aucun commentaire:
Enregistrer un commentaire