ECF 1.5
SimpleRandomizer.cpp
1#include "ECF_base.h"
2#include "SimpleRandomizer.h"
3#include <time.h>
4
5
6SimpleRandomizer::SimpleRandomizer(uint seed)
7{
8 if(seed == 0)
9 srand((unsigned int) time(NULL));
10 else
11 srand((unsigned int) seed);
12}
13
14
16{
17 return p + (int)( (double)(q-p+1) * (rand() / (RAND_MAX + 1.0)));
18}
19
20
22{
23 return (int)( (double)(size) * (rand() / (RAND_MAX + 1.0)));
24}
25
26
28{
29 return rand() / (double(RAND_MAX));
30}
31
32
33void SimpleRandomizer::registerParameters(StateP s)
34{
35 s->getRegistry()->registerEntry("randomizer.seed", (voidP) new uint(0), ECF::UINT,
36 "randomizer seed, 0 uses time(NULL) (default: 0)");
37}
38
39
41{
42 uint seed = 0;
43 if(state->getRegistry()->isModified("randomizer.seed")) {
44 voidP sptr = state->getRegistry()->getEntry("randomizer.seed");
45 seed = *((int*)sptr.get());
46 }
47
48 uint *memSeed = new uint;
49
50 if(seed != 0)
51 srand((unsigned int) seed);
52 else
53#ifdef _MPI
54 srand((uint) time(NULL) + state->getCommunicator()->getCommRank() + (uint) memSeed);
55#else
56 {
57 srand((unsigned int) time(NULL) + (unsigned int) *memSeed);
58 }
59#endif
60
61 delete memSeed;
62
63 return true;
64}
bool initialize(StateP s)
double getRandomDouble()
Returns random double.
int getRandomInteger(int p, int q)
Returns random integer in [p, q].