CGVIS Utils
Loading...
Searching...
No Matches
Rand.h
Go to the documentation of this file.
1#pragma once
2
3#include <random>
4#include <algorithm>
5
20class Random {
21public:
25 Random();
26
31 Random(uint32_t seed);
32
37 float rand005();
38
43 float rand051();
44
49 float rand01();
50
55 float rand11();
56
61 float rand0Pi();
62
74 template <typename T> T rand(T a, T b) {
75 return a+T(rand01()*(b-a));
76 }
77
84 template <typename T> void shuffle(std::vector<T>& a) {
85 for (size_t i=0;i<a.size();++i) {
86 size_t r = Random::rand<size_t>(0,i+1);
87 std::swap(a[i], a[r]);
88 }
89 }
90
91private:
93 std::random_device rd;
95 std::mt19937 gen;
97 std::uniform_real_distribution<float> dis01;
99 std::uniform_real_distribution<float> dis11;
101 std::uniform_real_distribution<float> disPi;
103 std::uniform_real_distribution<float> dis005;
105 std::uniform_real_distribution<float> dis051;
106
107};
108
116extern Random staticRand;
Random staticRand
Global PRNG instance for convenience functions.
Definition Rand.cpp:52
Definition Rand.h:20
void shuffle(std::vector< T > &a)
Shuffle a container in-place using Fisher–Yates.
Definition Rand.h:84
float rand005()
Draw a random float in [0, 0.5).
Definition Rand.cpp:36
float rand01()
Draw a random float in [0, 1).
Definition Rand.cpp:32
float rand051()
Draw a random float in [0.5, 1).
Definition Rand.cpp:40
float rand11()
Draw a random float in [-1, 1).
Definition Rand.cpp:44
T rand(T a, T b)
Draw a value uniformly in the half-open interval [a, b).
Definition Rand.h:74
Random()
Construct and seed from std::random_device.
Definition Rand.cpp:7
float rand0Pi()
Draw a random float in [0, ).
Definition Rand.cpp:48