reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofRandomEngine.h
Go to the documentation of this file.
1#ifndef OF_RANDOM_HPP_
2#define OF_RANDOM_HPP_
3
4#include <random>
5#include <glm/glm.hpp>
6
7#include "ofSingleton.hpp"
8#include "ofMath.h"
9
10namespace of::random
11{
12
13// https://stackoverflow.com/questions/25360241/using-random-number-generator-multiple-instances-or-singleton-approach
14// https://simplecxx.github.io/2018/11/03/seed-mt19937.html
15
23class Engine: public of::utils::Singleton<Engine> {
24
25 std::random_device rd_{ };
26 std::seed_seq seq_{ rd_(), rd_(), rd_(), rd_() }; // 4 is considered fine for non-cryptographic needs
27 std::mt19937 gen_{ seq_ };
28 bool deterministic_{ false }; // by default the degine is non-deterministic (unpredictable)
29
30public:
31
33 ofSeedRandom(); // called to maintain "parallelism" until old-school srand() is phase-out of OF
34 }
35
39 auto & gen() { return gen_; }
40
42 void seed(unsigned long new_seed) {
43 deterministic_ = true;
44 gen_.seed(new_seed);
45 }
46
48 auto is_deterministic() const { return deterministic_; }
49};
50
52inline auto engine() {
54}
55
57inline auto & gen() {
59}
60
62inline void seed(unsigned long seed) {
64}
65
67template<class T>
68void shuffle(T & values) {
69 std::shuffle(values.begin(), values.end(), of::random::gen());
70}
71
72} // end namespace of::random
73
74#endif // OF_RANDOM_HPP_
Definition ofRandomEngine.h:23
void seed(unsigned long new_seed)
passes a value to seed the mt19937 generator
Definition ofRandomEngine.h:42
auto & gen()
Definition ofRandomEngine.h:39
auto is_deterministic() const
Definition ofRandomEngine.h:48
Engine()
Definition ofRandomEngine.h:32
Definition ofSingleton.hpp:15
static Engine * instance()
Definition ofSingleton.hpp:31
Definition ofRandomDistributions.h:14
void seed(unsigned long seed)
Passes a value to seed the mt19937 generator within the ending instance.
Definition ofRandomEngine.h:62
auto & gen()
Definition ofRandomEngine.h:57
void shuffle(T &values)
Shuffles the order of the elements within the passed container, using the centralized random engine.
Definition ofRandomEngine.h:68
auto engine()
Definition ofRandomEngine.h:52
void ofSeedRandom()
Seeds the random number generator with a unique value.
Definition ofMath.cpp:19