Stride Reference Manual  1.0
Random.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * any later version.
7  * The software is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License
12  * along with the software. If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
15  */
16 
22 #include <limits>
23 
24 #include <trng/mrg2.hpp>
25 #include <trng/uniform01_dist.hpp>
26 #include <trng/uniform_int_dist.hpp>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 
31 namespace stride {
32 namespace util {
33 
34 inline double rateToProbability(double rate) {
35  return 1 - exp(-rate);
36 }
37 
39 class Random {
40 public:
41  Random(const unsigned long seed = 0) {
42  m_engine.seed(seed);
43  m_uniform_dist = trng::uniform01_dist<double>();
44  }
45 
46  double nextDouble() {
47  return m_uniform_dist(m_engine);
48  }
49 
51  unsigned int operator()(unsigned int max = std::numeric_limits<unsigned int>::max()) {
52  trng::uniform_int_dist dis(0, max);
53  return dis(m_engine);
54  }
55 
57  bool hasContact(double contact_rate) {
58  return nextDouble() < rateToProbability(contact_rate);
59  }
60 
62  bool hasTransmission(double transmission_rate) {
63  return nextDouble() < rateToProbability(transmission_rate);
64  }
65 
66  bool hasContactAndTransmission(double contact_rate, double transmission_rate) {
67  return nextDouble() < rateToProbability(transmission_rate * contact_rate);
68  }
69 
70  void setState(std::string state) {
71  std::stringstream ss;
72  ss.str(state);
73  ss >> m_engine;
74  }
75 
76  friend std::ostream& operator<<(std::ostream& os, const Random& random) {
77  os << random.m_engine;
78  return os;
79  }
80 
81 private:
82  trng::mrg2 m_engine;
83  trng::uniform01_dist<double> m_uniform_dist;
84 };
85 
86 
87 }
88 }
89 
void setState(std::string state)
Definition: Random.h:70
double rateToProbability(double rate)
Definition: Random.h:34
friend std::ostream & operator<<(std::ostream &os, const Random &random)
Definition: Random.h:76
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
bool hasContact(double contact_rate)
Check if two individuals have contact.
Definition: Random.h:57
bool hasContactAndTransmission(double contact_rate, double transmission_rate)
Definition: Random.h:66
bool hasTransmission(double transmission_rate)
Check if two individuals have transmission.
Definition: Random.h:62
Random(const unsigned long seed=0)
Definition: Random.h:41
trng::mrg2 m_engine
The random number engine.
Definition: Random.h:82
double nextDouble()
Definition: Random.h:46
trng::uniform01_dist< double > m_uniform_dist
The random distribution.
Definition: Random.h:83
The random number generator.
Definition: Random.h:39
unsigned int operator()(unsigned int max=std::numeric_limits< unsigned int >::max())
Get random unsigned int from [0, max[.
Definition: Random.h:51