PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
randomDist.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef UTILS_RANDOM_H
12#define UTILS_RANDOM_H
13
14#include <random>
15
16typedef std::mt19937 RandGenerator;
17typedef std::lognormal_distribution<> LogNormalDistribution;
18typedef std::uniform_real_distribution<> UniformDistribution;
19typedef std::normal_distribution<> NormalDistribution;
20typedef std::uniform_int_distribution<> UniformIntDistribution;
21
22namespace util {
23
30inline RandGenerator get_rd_gen(int seed = -1) {
31
32 //return RandGenerator();
33
34 if (seed < 0) {
35 std::random_device rd;
36 seed = rd();
37 }
38
39 return RandGenerator(seed);
40}
41
48inline std::default_random_engine get_rd_engine(int &seed) {
49
50 //return std::default_random_engine();
51
52 if (seed < 0) {
53 std::random_device rd;
54 seed = rd();
55 }
56
57 return std::default_random_engine(seed);
58}
59
68inline double transform_to_normal_dist(double mean, double std, double sample) {
69 return std * sample + mean;
70}
71
80inline double transform_to_uniform_dist(double min, double max, double sample) {
81 return min + sample * (max - min);
82}
83
89template<class T>
91public:
99 DistributionSample(double arg1, double arg2, int seed = -1)
100 : d_seed(seed), d_gen(get_rd_gen(seed)), d_dist(arg1, arg2){};
101
106
114 void init(double arg1, double arg2, int seed = -1) {
115
116 d_seed = seed;
118 d_dist = T(arg1, arg2);
119 }
120
125 double operator()() {
126 return d_dist(d_gen);
127 }
128
131
134
137};
138
139} // namespace util
140
141#endif // UTILS_RANDOM_H
Templated probability distribution.
Definition randomDist.h:90
T d_dist
Templated distribution.
Definition randomDist.h:136
DistributionSample()
Constructor.
Definition randomDist.h:105
DistributionSample(double arg1, double arg2, int seed=-1)
Constructor.
Definition randomDist.h:99
RandGenerator d_gen
Random number generator.
Definition randomDist.h:133
void init(double arg1, double arg2, int seed=-1)
Initialize the distribution.
Definition randomDist.h:114
double operator()()
Sample from the distribution.
Definition randomDist.h:125
Collection of methods useful in simulation.
double transform_to_uniform_dist(double min, double max, double sample)
Transform sample from U(0,1) to U(a,b)
Definition randomDist.h:80
std::default_random_engine get_rd_engine(int &seed)
Return random number generator.
Definition randomDist.h:48
double transform_to_normal_dist(double mean, double std, double sample)
Transform sample from N(0,1) to N(mean, std^2)
Definition randomDist.h:68
RandGenerator get_rd_gen(int seed=-1)
Return random number generator.
Definition randomDist.h:30
std::lognormal_distribution LogNormalDistribution
Definition randomDist.h:17
std::uniform_real_distribution UniformDistribution
Definition randomDist.h:18
std::normal_distribution NormalDistribution
Definition randomDist.h:19
std::mt19937 RandGenerator
Definition randomDist.h:16
std::uniform_int_distribution UniformIntDistribution
Definition randomDist.h:20