Revision f857128b

View differences:

include/casyncmodule.h
43 43
 */
44 44
class cAsyncMessage;
45 45

  
46
class SIM_API cAsyncModule : public cSimpleModule
46
class SIM_API cAsyncModule : public cSimpleModule, public cNumberGenerator
47 47
{
48 48
  private:
49 49

  
......
97 97
    void setMessageMetaData(cMessage* msg);
98 98

  
99 99
  protected:
100
    // the central number generator of this module
101
    cNumberGenerator* numGen;
102

  
103 100

  
104 101
  public:
105 102
    /** @name Constructors and Destructors */
......
262 259
     * Wrapper methods for numberGenerator Method calls
263 260
     *
264 261
     */
265

  
262
/*
266 263
    inline double uniform(double a, double b, int rng=0) {
267 264
        return numGen->uniform(a,b,rng);
268 265
    }
......
350 347
    inline double dblrand() {
351 348
        return numGen->dblrand();
352 349
    }
353

  
350
*/
354 351
};
355 352

  
356 353
#endif /* __CASYNCMODULE_H */
include/cnumgen.h
18 18

  
19 19
class cMersenneTwister;
20 20

  
21
class SIM_API cNumberGenerator : public cObject
21
class SIM_API cNumberGenerator // : public cObject
22 22
{
23 23
  private:
24 24

  
25
	int nrRngs;
25
	int nrRNGs;
26

  
26 27
	cRNG** rngs;
27 28

  
28 29
    /**
......
50 51
    /**
51 52
     * constructor
52 53
     */
53
    cNumberGenerator(unsigned nrNumGens,unsigned long * seeds);
54
    cNumberGenerator();
54 55

  
55 56
    /**
56 57
     * destructor
57 58
     */
58 59
    virtual ~cNumberGenerator();
59 60

  
61
    /*
62
     * Sets up seeds and instanciates the RNGs
63
     */
64
    void setupSeeds(unsigned nrRng, unsigned long * seeds);
65

  
60 66
    /**
61 67
     * @name Continuous distributions
62 68
     *
src/sim/casyncmodule.cc
68 68

  
69 69
cAsyncModule::~cAsyncModule()
70 70
{
71
  delete numGen;
71

  
72 72
}
73 73

  
74 74
void cAsyncModule::initLocalRNGs()
......
135 135
    //
136 136
    // generate the required number of RNGs with the corresponding seeds
137 137
    //
138
    numGen = new cNumberGenerator(nrNumGens,seeds);
138
    setupSeeds(nrNumGens,seeds);
139 139
    delete[] seeds;
140 140

  
141 141
}
src/sim/cnumgen.cc
34 34
#define M_E     2.7182818284590452353602874713527
35 35
#endif
36 36

  
37
cNumberGenerator::cNumberGenerator(unsigned nrRngs, unsigned long * seeds) :
38
	nrRngs(nrRngs) {
37
cNumberGenerator::cNumberGenerator() : nrRNGs(0) {
39 38
	if (simulation.isRunning()) {
40 39
		throw cRuntimeError(
41 40
				"Cannot (yet) create new instance of cNumberGenerator"
42 41
					" at runtime due to potentially concurrent access to"
43 42
					" the central seed generator.");
44 43
	}
45

  
46
	rngs = (cRNG**) new cMersenneTwister*[nrRngs];
47
	for (unsigned i = 0; i < nrRngs; i++) {
48
		rngs[i] = new cMersenneTwister();
49
		rngs[i]->seed(seeds[i]);
50
	}
51 44
}
52 45

  
53 46

  
54 47
cNumberGenerator::~cNumberGenerator() {
55
	for (int i = 0; i < nrRngs; i++)
48
	for (int i = 0; i < nrRNGs; i++)
56 49
		delete rngs[i];
57 50

  
58 51
}
59 52

  
60 53
void cNumberGenerator::checkBounds(int rngId) {
61
	if (nrRngs < rngId)
54
	if (nrRNGs <= rngId)
62 55
		throw cRuntimeError(
63
				"cNumberGenerator only has %d < %d local generators.", nrRngs,
56
				"cNumberGenerator only has %d < %d local generators.", nrRNGs,
64 57
				rngId);
65 58
}
59

  
60
void cNumberGenerator::setupSeeds(unsigned nrRng, unsigned long * seeds) {
61
    nrRNGs = nrRng;
62
    rngs = (cRNG**) new cMersenneTwister*[nrRNGs];
63
    for (int i = 0; i < nrRNGs; i++) {
64
        rngs[i] = new cMersenneTwister();
65
        rngs[i]->seed(seeds[i]);
66
    }
67
}
66 68
//----------------------------------------------------------------------------
67 69
//
68 70
//  C O N T I N U O U S

Also available in: Unified diff