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 */
25template <typename T> class Random
26{
27private:
28 unsigned int seed;
29 std::mt19937 gen;
30
31public:
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 */
43template <typename T>
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 */
57template <typename T>
58Random<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 */
72template <typename T>
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 */
87template <typename T>
88std::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}
Definition Random.h:26
T generateUniform(T lower=-1.0)
Definition Random.h:73
Random()
Definition Random.h:44