Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / clcg32.cc @ 3e29b8a0

History | View | Annotate | Download (7.58 KB)

1
//==========================================================================
2
//  CLCG32.CC - part of
3
//                 OMNeT++/OMNEST
4
//              Discrete System Simulation in C++
5
//
6
// Contents:
7
//   class cLCG32
8
//
9
//==========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 2002-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19
#include "clcg32.h"
20
#include "simutil.h"
21
#include "cenvir.h"
22
#include "cexception.h"
23
#include "cconfigoption.h"
24

    
25
USING_NAMESPACE
26

    
27

    
28
Register_Class(cLCG32);
29

    
30
Register_PerRunConfigOption(CFGID_SEED_N_LCG32, "seed-%-lcg32", CFG_INT, NULL, "When cLCG32 is selected as random number generator: seed for the kth RNG. (Substitute k for '%' in the key.)");
31
Register_GlobalConfigOption(CFGID_LCG32_SEED_GENERATOR_SEED, "seed-generator-seed", CFG_INT, "0", "The initial Seed for the Seed generator.");
32

    
33

    
34
void cLCG32::initializeAsMaster(int runNumber, int rngId, int numRngs,
35
                int parsimProcId, int parsimNumPartitions, cConfiguration *cfg) {
36

    
37
        unsigned long seedValue = cfg->getAsInt(CFGID_LCG32_SEED_GENERATOR_SEED);
38

    
39
        //
40
        // use runNumber etc. as usual if the user does not supply a special
41
        // seed for the seed generator.
42
        //
43
        //If default Value was returned
44
        if (seedValue == 0) {
45
                initialize(runNumber, rngId, numRngs, parsimProcId,
46
                                parsimNumPartitions, cfg);
47
                return;
48
        }
49

    
50
        seed(seedValue);
51
}
52

    
53
void cLCG32::initialize(int seedSet, int rngId, int numRngs,
54
                        int /*parsimProcId*/, int parsimNumPartitions,
55
                        cConfiguration *cfg)
56
{
57
    if (parsimNumPartitions>1)
58
        throw cRuntimeError("The cLCG32 RNG is not suitable for parallel simulation runs "
59
                            "because of its short cycle -- please select cMersenneTwister "
60
                            "in the configuration instead");
61

    
62
    char key[32];
63
    sprintf(key,  "seed-%d-lcg32", rngId);
64
    const char *value = cfg->getConfigValue(key);
65
    if (value==NULL)
66
    {
67
        int autoSeedIndex = seedSet*numRngs + rngId;
68
        if (autoSeedIndex>=256)
69
            ev << "Warning: LCG32: out of the 256 auto seed values, wrapping around "
70
                  "-- decrease num-rngs=" << numRngs << " value or run numbers, "
71
                  "or use a different RNG class like Mersenne Twister\n";
72
        autoSeedIndex = autoSeedIndex % 256;
73
        seedValue = autoSeeds[autoSeedIndex];
74
    }
75
    else
76
    {
77
        seedValue = cConfiguration::parseLong(value, 0);
78
        if (seedValue==0)
79
            throw cRuntimeError("cLCG32: zero is not allowed as seed in %s config file entry", key);
80
    }
81
}
82

    
83
void cLCG32::selfTest()
84
{
85
    seedValue = 1;
86
    for (int i=0; i<10000; i++)
87
        intRand();
88
    if (seedValue!=1043618065L)
89
        throw cRuntimeError("cLCG32: selfTest() failed, please report this problem!");
90
}
91

    
92
unsigned long cLCG32::intRand()
93
{
94
    numDrawn++;
95
    const long int a=16807, q=127773, r=2836;
96
    seedValue=a*(seedValue%q) - r*(seedValue/q);
97
    if (seedValue<=0) seedValue+=LCG32_MAX+1;
98
    return seedValue-1; // shift range [1..2^31-2] to [0..2^31-3]
99
}
100

    
101
unsigned long cLCG32::intRandMax()
102
{
103
    return LCG32_MAX-1; // 2^31-3
104
}
105

    
106
unsigned long cLCG32::intRand(unsigned long n)
107
{
108
    if (n>LCG32_MAX)
109
        throw cRuntimeError("cLCG32: intRand(%d): argument out of range 1..2^31-2");
110

    
111
    // code from MersenneTwister.h, Richard J. Wagner rjwagner@writeme.com
112
    // Find which bits are used in n
113
    unsigned long used = n-1;
114
    used |= used >> 1;
115
    used |= used >> 2;
116
    used |= used >> 4;
117
    used |= used >> 8;
118
    used |= used >> 16;
119

    
120
    // Draw numbers until one is found in [0,n]
121
    unsigned long i;
122
    do
123
        i = intRand() & used;  // toss unused bits to shorten search
124
    while( i >= n );
125
    return i;
126
}
127

    
128
double cLCG32::doubleRand()
129
{
130
    return (double)intRand() * (1.0/LCG32_MAX);
131
}
132

    
133
double cLCG32::doubleRandNonz()
134
{
135
    return (double)(intRand()+1) * (1.0/(LCG32_MAX+1));
136
}
137

    
138
double cLCG32::doubleRandIncl1()
139
{
140
    return (double)intRand() * (1.0/(LCG32_MAX-1));
141
}
142

    
143
void cLCG32::seed(unsigned long seed) {
144
        seedValue = seed;
145
}
146

    
147
long cLCG32::autoSeeds[] = {
148
    1L, 1331238991L, 1550655590L, 930627303L, 766698560L, 372156336L,
149
    1645116277L, 1635860990L, 1154667137L, 692982627L, 1961833381L,
150
    713190994L, 460575272L, 1298018763L, 1497719440L, 2030952567L,
151
    901595110L, 631930165L, 354421844L, 1213737427L, 1800697079L,
152
    795809157L, 821079954L, 1624537871L, 1918430133L, 861482464L,
153
    1736896562L, 1220028201L, 634729389L, 456922549L, 23246132L,
154
    979545543L, 1008653149L, 1156436224L, 1689665648L, 1604778030L,
155
    1628735393L, 1949577498L, 550023088L, 1726571906L, 1267216792L,
156
    1750609806L, 314116942L, 736299588L, 2095003524L, 1281569003L,
157
    356484144L, 1423591576L, 2140958617L, 1577658393L, 1116852912L,
158
    1865734753L, 1701937813L, 301264580L, 171817360L, 1809513683L,
159
    360646631L, 546534802L, 1652205397L, 136501886L, 605448579L,
160
    1857604347L, 1223969344L, 668104522L, 1821072732L, 738721927L,
161
    1237280745L, 1753702432L, 2125855022L, 1259255700L, 935058038L,
162
    1325807218L, 1151620124L, 585222105L, 1970906347L, 1267057970L,
163
    66562942L, 1959090863L, 1503754591L, 114059398L, 2007872839L,
164
    1886985293L, 1870986444L, 2110445215L, 1375265396L, 1512926254L,
165
    470646700L, 1951555990L, 500432100L, 1843528576L, 347147950L,
166
    1431668171L, 929595364L, 1507452793L, 800443847L, 1428656866L,
167
    5715478L, 1607979231L, 2032092669L, 37809413L, 996425830L,
168
    1010869813L, 1884232020L, 312192738L, 1821061493L, 462270727L,
169
    248900140L, 678804905L, 905130946L, 1892339752L, 1307421505L,
170
    491642575L, 1091346202L, 1076664921L, 1140141037L, 122447008L,
171
    1244153851L, 1382627577L, 611793617L, 1989326495L, 808278095L,
172
    1352281487L, 2106046913L, 1731628906L, 1226683843L, 1683200486L,
173
    90274853L, 1676390615L, 2147466840L, 498396356L, 2140522509L,
174
    1217803227L, 1146667727L, 788324559L, 1530171233L, 317473611L,
175
    319146380L, 992289339L, 2077765218L, 652681396L, 789950931L,
176
    485020132L, 632682054L, 32775496L, 1683083109L, 603834907L,
177
    351704670L, 1809710911L, 171230418L, 1511135464L, 1986612391L,
178
    1646573708L, 1411714374L, 1546459273L, 872179784L, 1307370996L,
179
    801917373L, 2051724276L, 144283230L, 1535180348L, 1949917822L,
180
    650872229L, 113201992L, 890256110L, 1965781805L, 1903960687L,
181
    679060319L, 452497769L, 630187802L, 174438105L, 1298843779L,
182
    961082145L, 1565131991L, 2078229636L, 50366922L, 959177042L,
183
    144513213L, 1423462005L, 207666443L, 152219823L, 13354949L,
184
    412643566L, 631135695L, 166938633L, 958408264L, 1324624652L,
185
    494931978L, 1472820641L, 1150735880L, 1508483704L, 1640573652L,
186
    359288909L, 1315013967L, 1051019865L, 1254156333L, 1883764098L,
187
    587564032L, 1288274932L, 1912367727L, 1595891993L, 2138169990L,
188
    1794668172L, 2059764593L, 1152025509L, 115613893L, 926625010L,
189
    131630606L, 706594585L, 1386707532L, 1624163092L, 2081362360L,
190
    1882152641L, 1428465736L, 602313149L, 1170668648L, 863700348L,
191
    931140599L, 1856765731L, 197473249L, 507314638L, 1381732824L,
192
    252975355L, 925311926L, 1726193892L, 576725369L, 774762078L,
193
    198434005L, 192355221L, 1296038143L, 1201667973L, 653782169L,
194
    1426685702L, 1503907840L, 211726157L, 33491376L, 906578176L,
195
    238345926L, 1826083853L, 1366925216L, 480315631L, 1549695660L,
196
    1337366022L, 1793656969L, 1469954017L, 1701980729L, 98857548L,
197
    1883864564L, 1709982325L, 251608257L, 1171967839L, 642486710L,
198
    1358844649L, 1115145546L, 1398997376L, 1021484058L, 2035865982L,
199
};
200

    
201