PyPO User Manual
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1
#include <random>
2
#include <iostream>
3
#include <string>
4
#include <vector>
5
6
#define _USE_MATH_DEFINES
7
8
#ifndef __Random_h
9
#define __Random_h
10
11
/*! \file Random.h
12
\brief Class for generating random numbers.
13
14
Generate object for creating random numbers for rejection sampling.
15
Currently only for Gaussian beams, but want to include Poisson disk sampling too.
16
If called empty, generate a random seed to use, which can be retrieved.
17
If called with seed, use that seed.
18
*/
19
20
/**
21
* Class for generating random numbers.
22
*
23
* Note that no function returns. All values are stored inside a variable which is passed by reference to the function.
24
*/
25
template
<
typename
T>
class
Random
26
{
27
private
:
28
unsigned
int
seed;
29
std::mt19937 gen;
30
31
public
:
32
Random
();
33
Random
(
unsigned
int
seed);
34
35
T
generateUniform
(T lower = -1.0);
36
std::vector<T>
generateUniform
(
int
num, T lower = -1.0);
37
};
38
#endif
39
40
/**
41
* Initialize RNG. This constructor generates a random seed for the random draws.
42
*/
43
template
<
typename
T>
44
Random<T>::Random
()
45
{
46
std::random_device rd;
47
std::mt19937 geno(rd());
48
this->seed = rd();
49
this->gen = geno;
50
}
51
52
/**
53
* Initialize RNG. This constructor takes a pre-set seed for the random draws.
54
*
55
* @param seed Positive integer determining the RNG seed.
56
*/
57
template
<
typename
T>
58
Random<T>::Random
(
unsigned
int
seed)
59
{
60
std::mt19937 geno(seed);
61
this->seed = seed;
62
this->gen = geno;
63
}
64
65
/**
66
* Generate a random sample.
67
*
68
* @param lower Lower value of range. Defaults to -1.0.
69
*
70
* @returns out Number between lower and 1.0..
71
*/
72
template
<
typename
T>
73
T
Random<T>::generateUniform
(T lower)
74
{
75
std::uniform_real_distribution<T> dis(lower, 1.0);
76
return
dis(this->gen);
77
}
78
79
/**
80
* Generate multiple random samples.
81
*
82
* @param num Number of samples to return
83
* @param lower Lower value of range. Defaults to -1.0.
84
*
85
* @returns out Vector containing num uniform samples between lower and 1.0.
86
*/
87
template
<
typename
T>
88
std::vector<T>
Random<T>::generateUniform
(
int
num, T lower)
89
{
90
std::uniform_real_distribution<T> dis(lower, 1.0);
91
std::vector<T> out(num, 0);
92
93
for
(
int
n = 0; n < num; ++n) {
94
out[n] = dis(this->gen);
95
}
96
return
out;
97
}
Random
Definition
Random.h:26
Random::generateUniform
T generateUniform(T lower=-1.0)
Definition
Random.h:73
Random::Random
Random()
Definition
Random.h:44
src
include
Random.h
Generated by
1.11.0