Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Random.hpp
1#pragma once
2
3#include "Common.hpp"
4#include <random>
5#include <mutex>
6
11class Random {
12public:
18 static void seed();
19
25 static void seed(unsigned int seedValue);
26
30 static unsigned int getSeed();
31
37 static int Int(int min, int max);
38
42 static float Float();
43
49 static float Float(float min, float max);
50
56 static glm::vec3 Color();
57
61 static glm::vec2 sampleDisk();
62
66 static glm::vec3 sampleSphere();
67
71 static glm::vec3 sampleCosineHemisphere();
72
76 template<typename T>
77 static void shuffle(std::vector<T> & items);
78
79private:
83 struct LocalMT19937 {
84
87
88 std::mt19937 mt;
89 unsigned int seed = 0;
90 };
91
92 static unsigned int _seed;
93 static std::mt19937 _shared;
94 static std::mutex _lock;
95 static thread_local LocalMT19937 _thread;
96};
97
98template<typename T>
99void Random::shuffle(std::vector<T> & items){
100 std::shuffle(items.begin(), items.end(), _thread.mt);
101}
Generate seedable random numbers of various types and in multiple intervals. Handles per-thread rando...
Definition: Random.hpp:11
static unsigned int getSeed()
Definition: Random.cpp:17
static glm::vec3 Color()
Definition: Random.cpp:34
static std::mt19937 _shared
Shared randomness generator, used for seeding per-thread generators.
Definition: Random.hpp:93
static glm::vec3 sampleCosineHemisphere()
Definition: Random.cpp:65
static glm::vec3 sampleSphere()
Definition: Random.cpp:58
static unsigned int _seed
The current main seed.
Definition: Random.hpp:92
static thread_local LocalMT19937 _thread
Per-thread randomness generator, seeded using the shared generator.
Definition: Random.hpp:95
static glm::vec2 sampleDisk()
Definition: Random.cpp:41
static int Int(int min, int max)
Definition: Random.cpp:21
static void seed()
Definition: Random.cpp:3
static void shuffle(std::vector< T > &items)
Definition: Random.hpp:99
static float Float()
Definition: Random.cpp:25
static std::mutex _lock
The lock for the shared generator.
Definition: Random.hpp:94
A MT19937 generator seeded using the shared generator. Used to provide per-thread MT19937 generators ...
Definition: Random.hpp:83
unsigned int seed
The local seed.
Definition: Random.hpp:89
std::mt19937 mt
The randomness generator.
Definition: Random.hpp:88
LocalMT19937()
Definition: Random.cpp:72