10 #include "xoroshiro.h" 12 #define tw_opi 6.28318530718 17 inline void tw_rand_init(uint32_t v, uint32_t w) {}
20 inline void tw_rand_init(uint32_t v, uint32_t w) { clcg4::init(v, w); }
24 inline double tw_rand_unif(G& g) {
25 return std::generate_canonical<double, std::numeric_limits<double>::digits>(g);
29 inline double tw_rand_unif(
clcg4& g) {
36 int64_t tw_rand_integer(G& g, int64_t low, int64_t high) {
40 return (low + (int64_t)(tw_rand_unif(g) * (high + 1 - low)));
47 uint64_t tw_rand_ulong(G& g, uint64_t low, uint64_t high) {
51 return (low + (uint64_t)(tw_rand_unif(g) * (high + 1 - low)));
56 int64_t tw_rand_binomial(G& g, int64_t N,
double P) {
59 for (
int trials = 0; trials < N; trials++) {
60 if (tw_rand_unif(g) <= P) {
69 int64_t tw_rand_geometric(G& g,
double P) {
72 while (tw_rand_unif(g) > P) {
80 double tw_rand_exponential(G& g,
double Lambda) {
81 return -Lambda * log(tw_rand_unif(g));
85 double tw_rand_pareto(G& g,
double scale,
double shape) {
86 return scale * 1.0/pow(tw_rand_unif(g), 1/shape);
90 double tw_rand_gamma(G& g,
double shape,
double scale) {
91 double a, b, q, phi, d;
94 a = 1 / sqrt(2 * shape - 1);
101 double U_One = tw_rand_unif(g);
102 double U_Two = tw_rand_unif(g);
103 double V = a * log(U_One / (1 - U_One));
104 double Y = shape * exp(V);
105 double Z = U_One * U_One * U_Two;
106 double W = b + q * V - Y;
108 double temp1 = W + d - phi * Z;
109 double temp2 = log(Z);
111 if (temp1 >= 0 || W >= temp2) {
115 }
else if (shape == 1) {
116 return (tw_rand_exponential(g, scale));
118 b = (exp(1) + shape) / exp(1);
121 double U_One = tw_rand_unif(g);
122 double P = b * U_One;
125 double Y = pow(P, (1 / shape));
126 double U_Two = tw_rand_unif(g);
128 if (U_Two <= exp(-Y)) {
132 double Y = -log((b - P) / shape);
133 double U_Two = tw_rand_unif(g);
135 if (U_Two <= pow(Y, (shape - 1))) {
145 template <
typename G>
146 double tw_rand_normal01(G& g) {
147 return (sqrt(-2.0 * log(tw_rand_unif(g))) * sin(tw_opi * tw_rand_unif(g)));
150 template <
typename G>
151 double tw_rand_normal_sd(G& g,
double Mu,
double Sd) {
152 return Mu + (tw_rand_normal01(g) * Sd);
155 template <
typename G>
156 double tw_rand_lognormal(G& g,
double mean,
double sd) {
157 return exp( mean + sd * tw_rand_normal01(g));
160 template <
typename G>
161 int64_t tw_rand_poisson(G& g,
double Lambda) {
162 double a = exp(-Lambda);
166 b = b * tw_rand_unif(g);
168 b = b * tw_rand_unif(g);
175 template <
typename G>
176 double tw_rand_weibull(G& g,
double mean,
double shape) {
177 double scale = mean / tgamma( ((
double)1.0 + (
double)1.0/shape));
178 return scale * pow(-log( tw_rand_unif(g)), (
double)1.0/shape);
Definition: xoroshiro.h:10
Implementation of a CLCG4 random number generator, based on the reversible implementation from ROSS (...
Definition: clcg4.h:14
Declares most types used within the simulator and by models.