CellModules
RandomManager.hpp
Go to the documentation of this file.
1
6#ifndef RANDOMMANAGER
7#define RANDOMMANAGER
8
9#include <cassert>
10#include <cmath>
11#include <mecacell/mecacell.h>
12#include <time.h>
13
14namespace Rand{
16 static std::uniform_real_distribution<double> probaGen(0.,1.);
17
20 }
26 static inline void initRandomSeed(int seed = clock()) {MecaCell::Config::globalRand().seed(seed);}
27
33 static inline double getProba(){return probaGen(MecaCell::Config::globalRand());}
34
42 static inline double getUniformDouble(double min, double max){
43 std::uniform_real_distribution<double> dist(min, max);
44 return dist(MecaCell::Config::globalRand());
45 }
46
54 static inline int getUniformInt(int min, int max){
55 std::uniform_int_distribution<int> dist(min, max);
56 return dist(MecaCell::Config::globalRand());
57 }
58
66 static inline double getNormalDouble(double mean, double var){
67 std::normal_distribution<double> dist(mean, var);
68 return dist(MecaCell::Config::globalRand());
69 }
70
77 static inline double getExponentialDouble(double lambda){
78 std::exponential_distribution<double> dist(lambda);
79 return dist(MecaCell::Config::globalRand());
80 }
81
91 static inline double getAsynchronousAge(double maxAge = 1.0){
92 assert(maxAge > 0.0);
93 static const double ln2 = std::log(2.0);
94 const double u = probaGen(MecaCell::Config::globalRand());
95 return maxAge * (-std::log1p(-0.5 * u) / ln2);
96 }
97
105 static inline double getBoundedNormalDouble(double mean, double bound){
106 std::normal_distribution<double> dist(mean, bound/3.);
107 return std::min(dist(MecaCell::Config::globalRand()), bound);
108 }
109
116 static inline double getLinearDouble(std::vector<std::pair<double,double>> values){
118 for (auto it = std::make_move_iterator(values.begin()),
119 end = std::make_move_iterator(values.end()); it != end; ++it){
120 i.push_back(std::move(it->first));
121 w.push_back(std::move(it->second));
122 }
123 std::piecewise_linear_distribution<double> dist(i.begin(), i.end(), w.begin());
124 return dist(MecaCell::Config::globalRand());
125 }
126
134 static inline double getLogNormalDouble(double mu, double sigma2){
135 std::lognormal_distribution<double> dist(mu, sigma2);
136 return dist(MecaCell::Config::globalRand());
137 }
138
146 static inline double getLogNormalDoubleMeanStd(double mean, double std){
147 double sigma2 = log(1+(std * std)/(mean * mean));
148 double mu = log(mean) - 0.5*sigma2;
149 return getLogNormalDouble(mu, sigma2);
150 }
151}
152
153#endif
A simple vector class template.
Definition: std.hpp:324
void push_back(const T &value)
Adds an element to the end of the vector.
Definition: std.hpp:338
iterator begin()
Returns an iterator to the first element.
Definition: std.hpp:443
iterator end()
Returns an iterator to the last element.
Definition: std.hpp:452
CompositeGenerator< T > values(T val1, T val2)
Definition: catch.hpp:1968
static std::uniform_real_distribution< double > probaGen(0., 1.)
@ignore
static double getLogNormalDouble(double mu, double sigma2)
Generates a random double in a log-normal distribution.
static double getProba()
Generates a random probability between 0 and 1.
static double getAsynchronousAge(double maxAge=1.0)
Generates an age for an asynchronous exponentially growing cell population.
static double getLogNormalDoubleMeanStd(double mean, double std)
Generates a random double in a log-normal distribution using mean and standard deviation.
static double getBoundedNormalDouble(double mean, double bound)
Generates a random double in a bounded normal distribution.
static int getUniformInt(int min, int max)
Generates a random integer in a uniform distribution.
static double getExponentialDouble(double lambda)
Generates a random double in an exponential distribution.
static void initRandomSeed(int seed=clock())
Initializes the random seed.
static double getUniformDouble(double min, double max)
Generates a random double in a uniform distribution.
static double getLinearDouble(std::vector< std::pair< double, double > > values)
Generates a random double in a piecewise linear distribution.
static double getNormalDouble(double mean, double var)
Generates a random double in a normal distribution.
static MecaCell::Config::random_engine_t & engine()
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
double max(double a, double b)
Computes the maximum of two numbers.
Definition: std.hpp:280
double log(double x)
Computes the natural logarithm of a number.
Definition: std.hpp:103
double min(double a, double b)
Computes the minimum of two numbers.
Definition: std.hpp:291
std::mt19937 random_engine_t
Definition: config.hpp:8
static random_engine_t & globalRand()
access to the static global random engine.This pseudo - random generator is* used in random 3D vector...
Definition: config.hpp:39