iLand
randomgenerator.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef RANDOMGENERATOR_H
21#define RANDOMGENERATOR_H
22#include <cstdlib>
23#include <math.h>
24#include <time.h>
25
26#define RANDOMGENERATORSIZE 500000
27#define RANDOMGENERATORROTATIONS 0
28// a new set of numbers is generated for every 5*500000 = 2.500.000 numbers
30{
31public:
35 static void setGeneratorType(const ERandomGenerators gen) { mGeneratorType = gen; mRotationCount=RANDOMGENERATORROTATIONS+1;mIndex=0;mRefillCounter=0; }
36 static void debugState(int &rIndex, int &rGeneration, int &rRefillCount) { rIndex = mIndex; rGeneration = mRotationCount; rRefillCount = mRefillCounter; }
37 static int debugNRandomNumbers() { return mIndex + RANDOMGENERATORSIZE*mRotationCount + (RANDOMGENERATORROTATIONS+1)*RANDOMGENERATORSIZE*mRefillCounter; }
40 static void checkGenerator() { if (mRotationCount>RANDOMGENERATORROTATIONS) { refill(); } }
41 static void setup(const ERandomGenerators gen, const unsigned oneSeed) { setGeneratorType(gen); seed(oneSeed); checkGenerator(); }
43 static void seed(const unsigned oneSeed);
45 static inline double rand() { return next() * (1.0/4294967295.0); }
46 static inline double rand(const double max_value) { return max_value * rand(); }
48 static inline unsigned long randInt(){ return next(); }
49 static inline unsigned long randInt(const int max_value) { return max_value>0?randInt()%max_value:0; }
51 static inline double randNorm( const double mean, const double stddev );
52
53private:
54 static inline unsigned long next() { ++mIndex; if (mIndex>RANDOMGENERATORSIZE) { mRotationCount++; mIndex=0; checkGenerator(); } return mBuffer[mIndex]; }
55 static unsigned int mBuffer[RANDOMGENERATORSIZE+5];
56 static int mIndex;
57 static int mRotationCount;
58 static int mRefillCounter;
59 static ERandomGenerators mGeneratorType;
60 static void refill();
61};
62
66
67
69inline double nrandom(const double& p1, const double& p2)
70{
71 return p1 + RandomGenerator::rand(p2-p1);
72 //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
73}
75inline double drandom()
76{
77 return RandomGenerator::rand();
78 //return rand()/double(RAND_MAX);
79}
81inline int irandom(int from, int to)
82{
83 return from + RandomGenerator::randInt(to-from);
84 //return from + rand()%(to-from);
85}
86
87
88
89
90inline double RandomGenerator::randNorm( const double mean, const double stddev )
91{
92 // Return a real number from a normal (Gaussian) distribution with given
93 // mean and standard deviation by polar form of Box-Muller transformation
94 double x, y, r;
95 do
96 {
97 x = 2.0 * rand() - 1.0;
98 y = 2.0 * rand() - 1.0;
99 r = x * x + y * y;
100 }
101 while ( r >= 1.0 || r == 0.0 );
102 double s = sqrt( -2.0 * log(r) / r );
103 return mean + x * s * stddev;
104}
105
106
107
108
109
110
111
112
113#endif // RANDOMGENERATOR_H
Definition: randomgenerator.h:30
static unsigned long randInt()
get a random integer in [0,2^32-1]
Definition: randomgenerator.h:48
static void seed(const unsigned oneSeed)
set a random generator seed. If oneSeed is 0, then a random number (provided by system time) is used ...
Definition: randomgenerator.cpp:167
static double rand(const double max_value)
Definition: randomgenerator.h:46
static void setup(const ERandomGenerators gen, const unsigned oneSeed)
Definition: randomgenerator.h:41
static void debugState(int &rIndex, int &rGeneration, int &rRefillCount)
Definition: randomgenerator.h:36
static double rand()
get a random value from [0., 1.]
Definition: randomgenerator.h:45
static void checkGenerator()
call this function to check if we need to create new random numbers.
Definition: randomgenerator.h:40
static int debugNRandomNumbers()
Definition: randomgenerator.h:37
static void setGeneratorType(const ERandomGenerators gen)
set the type of the random generator that should be used.
Definition: randomgenerator.h:35
ERandomGenerators
Definition: randomgenerator.h:32
@ ergFastRandom
Definition: randomgenerator.h:32
@ ergMersenneTwister
Definition: randomgenerator.h:32
@ ergWellRNG512
Definition: randomgenerator.h:32
@ ergXORShift96
Definition: randomgenerator.h:32
static double randNorm(const double mean, const double stddev)
Access to nonuniform random number distributions.
Definition: randomgenerator.h:90
static unsigned long randInt(const int max_value)
Definition: randomgenerator.h:49
RandomGenerator()
Definition: randomgenerator.h:33
double drandom()
returns a random number in [0,1] (i.e.="1" is a possible result!)
Definition: randomgenerator.h:75
#define RANDOMGENERATORROTATIONS
Definition: randomgenerator.h:27
int irandom(int from, int to)
return a random number from "from" to "to" (excluding 'to'.), i.e. irandom(3,6) results in 3,...
Definition: randomgenerator.h:81
double nrandom(const double &p1, const double &p2)
nrandom returns a random number from [p1, p2] -> p2 is a possible result!
Definition: randomgenerator.h:69
#define RANDOMGENERATORSIZE
Definition: randomgenerator.h:26