VoronoiGrowthSimulator
Based on Site Saturated Nucleation and Isotropic Growth
Loading...
Searching...
No Matches
MTEngine Class Reference

A class that represents a random number generator engine. More...

#include <MTEngine.hpp>

Collaboration diagram for MTEngine:

Public Member Functions

 MTEngine ()
 Constructs an instance of the MTEngine class.
 
 MTEngine (long long myseed)
 Constructs an instance of the MTEngine class with the specified seed.
 
void reseed ()
 
void randomseed ()
 
void setseed (long long myseed)
 
double rand ()
 
double randn ()
 
double randn (double mean, double var)
 
double choice (double a, double b)
 
double choice (double a, double b, double c)
 
template<typename T , typename... Args>
Choice (T Start, Args... args)
 
template<typename T >
choice (T A, T B)
 
double choice (std::vector< double > A, std::vector< double > PDF)
 
std::vector< double > rand (int m)
 
std::vector< double > randn (int m)
 
std::vector< std::vector< double > > rand (int m, int n)
 
std::vector< std::vector< double > > randn (int m, int n)
 
std::vector< double > rand (double a, double b, int p)
 
double rand (double a, double b)
 
int randint (int a, int b)
 
std::vector< int > randint (int a, int b, int p, bool repeatation=false)
 
std::vector< std::vector< int > > randint (int xmin, int xmax, int ymin, int ymax, int p, bool repeatation=false)
 
std::vector< std::array< double, 3 > > randint (int xmin, int xmax, int ymin, int ymax, int zmin, int zmax, int p, bool repeatation=false)
 
std::vector< int > sequence (int a, int b)
 
std::vector< int > ones (int n)
 
template<class T >
void Shuffle (std::vector< T > &X)
 

Public Attributes

long long wasseededwith
 

Detailed Description

A class that represents a random number generator engine.

The MTEngine class provides various methods for generating random numbers and sequences. It is based on the Mersenne Twister algorithm (mt19937_64).

Definition at line 21 of file MTEngine.hpp.

Constructor & Destructor Documentation

◆ MTEngine() [1/2]

MTEngine::MTEngine ( )

Constructs an instance of the MTEngine class.

This constructor initializes the random number generator (RNG) of the MTEngine object. It uses the current time as the seed for the RNG.

Note
The RNG is seeded with the current time using std::chrono::high_resolution_clock.
See also
MTEngine::rng
MTEngine::wasseededwith

Definition at line 32 of file MTEngine.cpp.

33{
34 long long myseed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
35 //std::seed_seq ss{ uint32_t(myseed & 0xffffffff), uint32_t(myseed >> 32) };
36 //rng.seed(ss);
37 rng.seed(myseed);
38 wasseededwith = myseed;
39}
long long wasseededwith
Definition MTEngine.hpp:29

◆ MTEngine() [2/2]

MTEngine::MTEngine ( long long myseed)

Constructs an instance of the MTEngine class with the specified seed.

This constructor initializes the random number generator (RNG) with the given seed. The seed is used to determine the initial state of the RNG, which affects the sequence of random numbers generated by the engine.

Parameters
myseedThe seed value to initialize the RNG.

Definition at line 15 of file MTEngine.cpp.

16{
17 rng.seed(myseed);
18 wasseededwith = myseed;
19}

Member Function Documentation

◆ choice() [1/4]

double MTEngine::choice ( double a,
double b )

Returns a random choice between two given values.

Parameters
aThe first value.
bThe second value.
Returns
The randomly chosen value between a and b.

Definition at line 97 of file MTEngine.cpp.

98{
99 double r = rand();
100 if (r<=0.5)
101 {
102 return a;
103 }
104 else
105 {
106 return b;
107 }
108 return a;
109}
double rand()
Definition MTEngine.cpp:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ choice() [2/4]

double MTEngine::choice ( double a,
double b,
double c )

Returns one of the three input values randomly based on a uniform distribution.

Parameters
aThe first input value.
bThe second input value.
cThe third input value.
Returns
One of the input values randomly chosen based on a uniform distribution.

Definition at line 119 of file MTEngine.cpp.

120{
121 double r = rand();
122 if (r <= 1.0/3.0)
123 {
124 return a;
125 }
126 else if (r<=2.0/3.0)
127 {
128 return b;
129 }
130 else
131 {
132 return c;
133 }
134 return a;
135}
Here is the call graph for this function:

◆ choice() [3/4]

double MTEngine::choice ( std::vector< double > A,
std::vector< double > PDF )

Returns a random choice from the given set of options based on the provided probability distribution.

Parameters
AThe set of options to choose from.
PDFThe probability distribution corresponding to each option.
Returns
The randomly chosen option.

Definition at line 148 of file MTEngine.cpp.

149{
150 unsigned int points = A.size();
151 std::vector<double> CDF(points, 0);
152
153 CDF[0] = PDF[0];
154 for (size_t i = 0; i < points-1; i++)
155 {
156 CDF[i + 1] = CDF[i] + PDF[i];
157 }
158
159 double r = CDF[points-1]*rand();
160
161 if (r<CDF[0])
162 {
163 return A[0];
164 }
165
166 else
167 {
168 for (size_t i = 0; i < points-1; i++)
169 {
170 if (r>=CDF[i] && r < CDF[i+1])
171 {
172 return A[i+1];
173 }
174
175 }
176 }
177}
Here is the call graph for this function:

◆ choice() [4/4]

template<typename T >
T MTEngine::choice ( T A,
T B )
inline

Makes a random choice between two values.

Parameters
AThe first value to choose from.
BThe second value to choose from.
Returns
The randomly chosen value between A and B.

Definition at line 82 of file MTEngine.hpp.

83{
84 double r = this->rand();
85 if (r <= 0.5)
86 return A;
87 else
88 return B;
89}
Here is the call graph for this function:

◆ Choice()

template<typename T , typename... Args>
T MTEngine::Choice ( T Start,
Args... args )
inline

Returns a randomly chosen element from a list of arguments.

Parameters
StartThe first element in the list.
argsThe remaining elements in the list.
Returns
The randomly chosen element.
Exceptions
std::runtime_errorif no match is found.

Definition at line 100 of file MTEngine.hpp.

101{
102 double r = rand();
103 const std::size_t n = sizeof...(Args) + 1;
104 if (r <= 1.0 / n)
105 {
106 return Start;
107 }
108 int I = 0;
109 int i = 2;
110 while (i<=n)
111 {
112 if (r <= double(i) / n)
113 {
114 I = i - 1;
115 break;
116 }
117 i++;
118 }
119
120 int count = 1;
121 for (const auto p : { args... }) {
122 if (count == I)
123 {
124 return p;
125 }
126 count++;
127 }
128
129 throw std::runtime_error("No match found in Choice method.");
130}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ones()

vector< int > MTEngine::ones ( int n)

Returns a vector of size n with all elements set to 1.

Parameters
nThe size of the vector.
Returns
A vector of size n with all elements set to 1.

Definition at line 520 of file MTEngine.cpp.

521{
522 vector<int> I(n, 0);
523 for (int i = 0; i < n; i++)
524 {
525 I[i] = 1;
526 }
527 return I;
528}

◆ rand() [1/5]

double MTEngine::rand ( )

Generates a random double value between 0 and 1 using the Mersenne Twister engine.

Returns
The generated random double value.

Definition at line 84 of file MTEngine.cpp.

85{
86 std::uniform_real_distribution<double> unif(0, 1);
87 return unif(rng);
88}
Here is the caller graph for this function:

◆ rand() [2/5]

double MTEngine::rand ( double a,
double b )

Generates a random number between the given range [a, b].

Parameters
aThe lower bound of the range.
bThe upper bound of the range.
Returns
A random number between the range [a, b].

Definition at line 320 of file MTEngine.cpp.

321{
322 double r = rand();
323
324 return r * a + (1 - r) * b;
325}
Here is the call graph for this function:

◆ rand() [3/5]

vector< double > MTEngine::rand ( double a,
double b,
int p )

Generates a vector of random double values within the specified range.

Parameters
aThe lower bound of the range.
bThe upper bound of the range.
pThe number of random values to generate.
Returns
A vector of random double values.

Definition at line 294 of file MTEngine.cpp.

295{
296 if (a > b)
297 {
298 double tmp1 = a;
299 b = a;
300 a = tmp1;
301 }
302
303 // initialize a uniform distribution between a and b
304 std::uniform_real_distribution<double> unif(a, b);
305 vector<double> A(p, 0.0);
306 for (int i = 0; i < p; i++)
307 {
308 A[i] = unif(rng);
309 }
310 return A;
311}

◆ rand() [4/5]

vector< double > MTEngine::rand ( int m)

Generates a vector of random numbers between 0 and 1 using a uniform distribution.

Parameters
mThe number of random numbers to generate.
Returns
A vector of random numbers.

Definition at line 210 of file MTEngine.cpp.

211{
212 // initialize a uniform distribution between a and b
213 std::uniform_real_distribution<double> unif(0, 1);
214 vector<double> A(m, 0.0);
215 for (int i = 0; i < m; i++)
216 {
217 A[i] = unif(rng);
218 }
219 return A;
220}

◆ rand() [5/5]

vector< vector< double > > MTEngine::rand ( int m,
int n )

Generates a random matrix of size m x n.

Parameters
mThe number of rows in the matrix.
nThe number of columns in the matrix.
Returns
A vector of vectors representing the random matrix.

Definition at line 247 of file MTEngine.cpp.

248{
249 std::vector<std::vector<double>> matrix;
250 matrix.resize(m, std::vector<double>(n, 0.0)); //Initialized With 0.0
251 std::uniform_real_distribution<double> unif(0, 1);
252 for (size_t i = 0; i < m; i++)
253 {
254 for (size_t j = 0; j < n; j++)
255 {
256 matrix[i][j] = unif(rng);
257 }
258 }
259
260 return matrix;
261}

◆ randint() [1/4]

int MTEngine::randint ( int a,
int b )

Generates a random integer between the specified range [a, b].

Parameters
aThe lower bound of the range.
bThe upper bound of the range.
Returns
A random integer between a and b (inclusive).

Definition at line 334 of file MTEngine.cpp.

335{
336 // initialize a uniform distribution between a and b
337 std::uniform_int_distribution<int> unif(a, b);
338 return unif(rng);
339}

◆ randint() [2/4]

vector< int > MTEngine::randint ( int a,
int b,
int p,
bool repeatation = false )

Generates a vector of random integers within a specified range.

Parameters
aThe lower bound of the range (inclusive).
bThe upper bound of the range (inclusive).
pThe number of random integers to generate.
repeatationDetermines whether repeated values are allowed in the generated vector. If set to true, repeated values are allowed; otherwise, each value in the vector will be unique.
Returns
A vector of random integers within the specified range.

Definition at line 351 of file MTEngine.cpp.

352{
353 // initialize a uniform distribution between a and b
354 std::uniform_int_distribution<int> unif(a, b);
355 vector<int> A(p, 0);
356 if (repeatation == true)
357 {
358 for (int i = 0; i < p; i++)
359 {
360 A[i] = unif(rng);
361 }
362 }
363 else
364 {
365 vector<bool> Auxilary(p, false);
366 int count = 0;
367 while (count < p)
368 {
369 int proposed = unif(rng);
370 if (Auxilary[proposed] == 0)
371 {
372 A[count] = proposed;
373 Auxilary[proposed] = 1;
374 count++;
375 }
376 }
377 }
378
379 return A;
380}

◆ randint() [3/4]

vector< vector< int > > MTEngine::randint ( int xmin,
int xmax,
int ymin,
int ymax,
int p,
bool repeatation = false )

Generates a 2D vector of random integers within specified ranges.

Parameters
xminThe minimum value for the x-coordinate.
xmaxThe maximum value for the x-coordinate.
yminThe minimum value for the y-coordinate.
ymaxThe maximum value for the y-coordinate.
pThe number of points to generate.
repeatationDetermines whether repeated points are allowed or not.
Returns
A 2D vector containing the generated random points.

Definition at line 393 of file MTEngine.cpp.

394{
395 // initialize a uniform distribution
396 std::uniform_int_distribution<int> unifx(xmin, xmax);
397 std::uniform_int_distribution<int> unify(ymin, ymax);
398
399 int rowCount = xmax - xmin + 1;
400 int colCount = ymax - ymin + 1;
401
402 std::vector<std::vector<int>> points;
403 points.resize(p, std::vector<int>(2, 0)); //Initialized With 0
404
405 if (repeatation == true)
406 {
407 for (size_t i = 0; i < p; i++)
408 {
409 points[i][0] = unifx(rng);
410 points[i][1] = unifx(rng);
411 }
412 }
413
414 else
415 {
416 std::vector<std::vector<bool>> Auxilary;
417 Auxilary.resize(rowCount, std::vector<bool>(colCount, 0.0)); //Initialized With 0.0
418 int count = 0;
419 while (count < p)
420 {
421 int propx = unifx(rng);
422 int propy = unify(rng);
423 if (Auxilary[propx][propy] == 0)
424 {
425 points[count][0] = propx;
426 points[count][1] = propy;
427 Auxilary[propx][propy] = 1;
428 count++;
429 }
430 }
431 }
432 return points;
433}

◆ randint() [4/4]

vector< std::array< double, 3 > > MTEngine::randint ( int xmin,
int xmax,
int ymin,
int ymax,
int zmin,
int zmax,
int p,
bool repeatation = false )

Generates a vector of random points within specified ranges.

Parameters
xminThe minimum value for the x-coordinate.
xmaxThe maximum value for the x-coordinate.
yminThe minimum value for the y-coordinate.
ymaxThe maximum value for the y-coordinate.
zminThe minimum value for the z-coordinate.
zmaxThe maximum value for the z-coordinate.
pThe number of points to generate.
repeatationDetermines whether repeated points are allowed.
Returns
A vector of arrays representing the generated points.

Definition at line 448 of file MTEngine.cpp.

449{
450 // initialize a uniform distribution
451 std::uniform_int_distribution<int> unifx(xmin, xmax);
452 std::uniform_int_distribution<int> unify(ymin, ymax);
453 std::uniform_int_distribution<int> unifz(zmin, zmax);
454
455 int X1 = xmax - xmin + 1;
456 int X2 = ymax - ymin + 1;
457 int X3 = zmax - zmin + 1;
458 int X1X2 = X1 * X2;
459 int X1X2X3 = X1 * X2*X3;
460
461 std::vector<std::array<double, 3>> points(p);
462 //points.resize(p, std::vector<int>(3, 0)); //Initialized With 0
463
464 if (repeatation == true)
465 {
466 for (size_t i = 0; i < p; i++)
467 {
468 points[i][0] = unifx(rng);
469 points[i][1] = unify(rng);
470 points[i][2] = unifz(rng);
471 }
472 }
473
474 else
475 {
476 vector<int> seq = sequence(0, X1X2X3);
477 Shuffle(seq);
478#pragma omp parallel for
479 for (int i = 0; i < p; i++)
480 {
481 int zo = seq[i] / X1X2;
482 int diff = seq[i] - X1X2 * zo;
483 int yo = diff / X1;
484 int xo = diff - X1 * yo;
485 points[i][0] = xmin + xo;
486 points[i][1] = ymin + yo;
487 points[i][2] = zmin + zo;
488 }
489 }
490
491 return points;
492}
std::vector< int > sequence(int a, int b)
Definition MTEngine.cpp:501
void Shuffle(std::vector< T > &X)
Definition MTEngine.hpp:67
Here is the call graph for this function:

◆ randn() [1/4]

double MTEngine::randn ( )

Generates a random number from a normal distribution with mean 0 and standard deviation 1.

Returns
The generated random number.

Definition at line 186 of file MTEngine.cpp.

187{
188 std::normal_distribution<double> gauss(0, 1);
189 return gauss(rng);
190}

◆ randn() [2/4]

double MTEngine::randn ( double mean,
double var )

Generates a random number from a normal distribution with the specified mean and variance.

Parameters
meanThe mean of the normal distribution.
varThe variance of the normal distribution.
Returns
A random number from the normal distribution.

Definition at line 199 of file MTEngine.cpp.

200{
201 std::normal_distribution<double> gauss(mean, var);
202 return gauss(rng);
203}

◆ randn() [3/4]

vector< double > MTEngine::randn ( int m)

Generates a vector of random numbers from a standard normal distribution.

Parameters
mThe number of random numbers to generate.
Returns
A vector of random numbers.

Definition at line 228 of file MTEngine.cpp.

229{
230 std::normal_distribution<double> gauss(0, 1);
231 vector<double> A(m, 0.0);
232#pragma omp parallel for
233 for (int i = 0; i < m; i++)
234 {
235 A[i] = gauss(rng);
236 }
237 return A;
238}

◆ randn() [4/4]

vector< vector< double > > MTEngine::randn ( int m,
int n )

Generates a matrix of random numbers from a standard normal distribution.

Parameters
mThe number of rows in the matrix.
nThe number of columns in the matrix.
Returns
A matrix of size m x n filled with random numbers from a standard normal distribution.

Definition at line 270 of file MTEngine.cpp.

271{
272 std::vector<std::vector<double>> matrix;
273 matrix.resize(m, std::vector<double>(n, 0.0)); //Initialized With 0.0
274 std::normal_distribution<double> gauss(0, 1);
275 for (size_t i = 0; i < m; i++)
276 {
277 for (size_t j = 0; j < n; j++)
278 {
279 matrix[i][j] = gauss(rng);
280 }
281 }
282
283 return matrix;
284}

◆ randomseed()

void MTEngine::randomseed ( )

Seeds the random number generator with a random value based on the current time. This function uses the high-resolution clock to generate a seed value. The generated seed is then used to seed the random number generator. The seed value is also stored in the wasseededwith member variable.

Definition at line 59 of file MTEngine.cpp.

60{
61 long long myseed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
62 //std::seed_seq ss{ uint32_t(myseed & 0xffffffff), uint32_t(myseed >> 32) };
63 //rng.seed(ss);
64 rng.seed(myseed);
65 wasseededwith = myseed;
66}

◆ reseed()

void MTEngine::reseed ( )

Reseeds the random number generator with the specified seed value.

Parameters
None
Returns
None

Definition at line 48 of file MTEngine.cpp.

49{
50 rng.seed(wasseededwith);
51}

◆ sequence()

vector< int > MTEngine::sequence ( int a,
int b )

Generates a sequence of integers between 'a' and 'b'.

Parameters
aThe starting value of the sequence.
bThe ending value of the sequence.
Returns
A vector containing the generated sequence.

Definition at line 501 of file MTEngine.cpp.

502{
503 int p = b - a;
504 vector<int> I(p, 0);
505
506#pragma omp parallel for
507 for (int i = 0; i < p; i++)
508 {
509 I[i] = a + i;
510 }
511 return I;
512}
Here is the caller graph for this function:

◆ setseed()

void MTEngine::setseed ( long long myseed)

Sets the seed for the random number generator used by the MTEngine.

Parameters
myseedThe seed value to set for the random number generator.

Definition at line 73 of file MTEngine.cpp.

74{
75 rng.seed(myseed);
76 wasseededwith = myseed;
77}

◆ Shuffle()

template<class T >
void MTEngine::Shuffle ( std::vector< T > & X)
inline

Shuffles the elements in the given vector using the random number generator.

Parameters
XThe vector to be shuffled.

Definition at line 67 of file MTEngine.hpp.

68{
69 std::shuffle(X.begin(), X.end(), rng);
70}
Here is the caller graph for this function:

Member Data Documentation

◆ wasseededwith

long long MTEngine::wasseededwith

Definition at line 29 of file MTEngine.hpp.


The documentation for this class was generated from the following files: