NcEngine
Random.h
Go to the documentation of this file.
1
5#pragma once
6
8#include "ncengine/type/EngineId.h"
9
10#include "ncmath/Vector.h"
11#include "ncmath/Xoshiro256.h"
12
13#include <random>
14
15namespace nc
16{
24class Random : public Module
25{
26 public:
30 explicit Random() noexcept
31 : Random(std::random_device{}())
32 {
33 }
34
39 explicit Random(uint64_t seed) noexcept
40 : Module{NcRandomId},
41 m_seed{seed},
42 m_generator{m_seed},
43 m_distribution{0.0f, std::nextafter(1.0f, std::numeric_limits<float>::max())}
44 {
45 }
46
51 auto Fork() noexcept { return Random{GetU64()}; }
52
57 void Seed(uint64_t seed) noexcept { *this = Random{seed}; }
58
63 auto Seed() const noexcept { return m_seed; }
64
69 auto GetGenerator() -> Xoshiro256ss& { return m_generator; }
70
75 auto Get() noexcept -> float { return m_distribution(m_generator); }
76
81 auto GetU64() noexcept -> uint64_t { return m_generator(); }
82
87 auto GetVector2() noexcept -> Vector2 { return Vector2{Get(), Get()}; }
88
93 auto GetVector3() noexcept -> Vector3 { return Vector3{Get(), Get(), Get()}; }
94
99 auto GetVector4() noexcept -> Vector4 { return Vector4{Get(), Get(), Get(), Get()}; }
100
107 auto Between(float min, float max) noexcept -> float { return Get() * (max - min) + min; }
108
115 auto Between(uint64_t min, uint64_t max) noexcept -> uint64_t
116 {
117 auto distribution = std::uniform_int_distribution<uint64_t>{min, max};
118 return distribution(m_generator);
119 }
120
127 auto Between(const Vector2& min, const Vector2& max) noexcept -> Vector2 { return HadamardProduct(GetVector2(), max - min) + min; }
128
135 auto Between(const Vector3& min, const Vector3& max) noexcept -> Vector3 { return HadamardProduct(GetVector3(), max - min) + min; }
136
143 auto Between(const Vector4& min, const Vector4& max) noexcept -> Vector4 { return HadamardProduct(GetVector4(), max - min) + min; }
144
149 friend bool operator==(const Random& lhs, const Random& rhs) noexcept { return lhs.m_generator == rhs.m_generator; }
150
155 friend bool operator!=(const Random& lhs, const Random& rhs) noexcept { return lhs.m_generator != rhs.m_generator; }
156
157 private:
158 uint64_t m_seed;
159 Xoshiro256ss m_generator;
160 std::uniform_real_distribution<float> m_distribution;
161};
162} // namespace nc
Modules are extensions that provide functionality to the engine.
Definition: Module.h:19
Random number generator.
Definition: Random.h:25
Random() noexcept
Default construct a new Random object.
Definition: Random.h:30
friend bool operator!=(const Random &lhs, const Random &rhs) noexcept
Check if two generators have different states.
Definition: Random.h:155
auto GetU64() noexcept -> uint64_t
Generate a random uint64_t in the range [0, std::numeric_limits<uint64_t>::max()].
Definition: Random.h:81
auto GetVector3() noexcept -> Vector3
Generate a random Vector3 with components in the range [0, 1].
Definition: Random.h:93
auto Fork() noexcept
Returns a new Random instance seeded with the next generator value.
Definition: Random.h:51
auto Get() noexcept -> float
Generate a random float in the range [0, 1].
Definition: Random.h:75
auto Between(uint64_t min, uint64_t max) noexcept -> uint64_t
Generate a random uint64_t in the range [min, max].
Definition: Random.h:115
auto Seed() const noexcept
Get the current seed.
Definition: Random.h:63
auto GetGenerator() -> Xoshiro256ss &
Get the underlying generator.
Definition: Random.h:69
friend bool operator==(const Random &lhs, const Random &rhs) noexcept
Check if two generators have the same state.
Definition: Random.h:149
auto Between(const Vector3 &min, const Vector3 &max) noexcept -> Vector3
Generate a random Vector3 with components in the range [min, max].
Definition: Random.h:135
auto GetVector4() noexcept -> Vector4
Generate a random Vector4 with components in the range [0, 1].
Definition: Random.h:99
auto Between(const Vector2 &min, const Vector2 &max) noexcept -> Vector2
Generate a random Vector2 with components in the range [min, max].
Definition: Random.h:127
auto Between(const Vector4 &min, const Vector4 &max) noexcept -> Vector4
Generate a random Vector4 with components in the range [min, max].
Definition: Random.h:143
void Seed(uint64_t seed) noexcept
Seed the generator.
Definition: Random.h:57
auto GetVector2() noexcept -> Vector2
Generate a random Vector2 with components in the range [0, 1].
Definition: Random.h:87
auto Between(float min, float max) noexcept -> float
Generate a random float in the range [min, max].
Definition: Random.h:107
Random(uint64_t seed) noexcept
Construct a new Random object from a seed.
Definition: Random.h:39
Random number engine based on Xoshiro256** by David Blackman and Sebastiano Vigna.
Definition: Xoshiro256.h:11
A two component vector.
Definition: Vector.h:13
A three component vector.
Definition: Vector.h:29
A four component vector.
Definition: Vector.h:48