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(size_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(size_t seed) noexcept { *this = Random{seed}; }
58
63 auto Seed() const noexcept { return m_seed; }
64
69 auto Get() noexcept -> float { return m_distribution(m_generator); }
70
75 auto GetU64() noexcept -> size_t { return m_generator(); }
76
81 auto GetVector2() noexcept -> Vector2 { return Vector2{Get(), Get()}; }
82
87 auto GetVector3() noexcept -> Vector3 { return Vector3{Get(), Get(), Get()}; }
88
93 auto GetVector4() noexcept -> Vector4 { return Vector4{Get(), Get(), Get(), Get()}; }
94
101 auto Between(float min, float max) noexcept -> float { return Get() * (max - min) + min; }
102
109 auto Between(size_t min, size_t max) noexcept -> size_t
110 {
111 const auto minFloat = static_cast<float>(min);
112 const auto maxFloat = static_cast<float>(max);
113 return static_cast<size_t>(Get() * (maxFloat - minFloat) + minFloat);
114 }
115
122 auto Between(const Vector2& min, const Vector2& max) noexcept -> Vector2 { return HadamardProduct(GetVector2(), max - min) + min; }
123
130 auto Between(const Vector3& min, const Vector3& max) noexcept -> Vector3 { return HadamardProduct(GetVector3(), max - min) + min; }
131
138 auto Between(const Vector4& min, const Vector4& max) noexcept -> Vector4 { return HadamardProduct(GetVector4(), max - min) + min; }
139
144 friend bool operator==(const Random& lhs, const Random& rhs) noexcept { return lhs.m_generator == rhs.m_generator; }
145
150 friend bool operator!=(const Random& lhs, const Random& rhs) noexcept { return lhs.m_generator != rhs.m_generator; }
151
152 private:
153 size_t m_seed;
154 Xoshiro256ss m_generator;
155 std::uniform_real_distribution<float> m_distribution;
156};
157} // namespace nc
Modules are extensions that provide functionality to the engine.
Definition: Module.h:18
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:150
auto GetVector3() noexcept -> Vector3
Generate a random Vector3 with components in the range [0, 1].
Definition: Random.h:87
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:69
auto GetU64() noexcept -> size_t
Generate a random size_t in the range [0, std::numeric_limits<size_t>::max()].
Definition: Random.h:75
auto Seed() const noexcept
Get the current seed.
Definition: Random.h:63
friend bool operator==(const Random &lhs, const Random &rhs) noexcept
Check if two generators have the same state.
Definition: Random.h:144
auto Between(const Vector3 &min, const Vector3 &max) noexcept -> Vector3
Generate a random Vector3 with components in the range [min, max].
Definition: Random.h:130
auto GetVector4() noexcept -> Vector4
Generate a random Vector4 with components in the range [0, 1].
Definition: Random.h:93
auto Between(const Vector2 &min, const Vector2 &max) noexcept -> Vector2
Generate a random Vector2 with components in the range [min, max].
Definition: Random.h:122
auto Between(const Vector4 &min, const Vector4 &max) noexcept -> Vector4
Generate a random Vector4 with components in the range [min, max].
Definition: Random.h:138
auto GetVector2() noexcept -> Vector2
Generate a random Vector2 with components in the range [0, 1].
Definition: Random.h:81
void Seed(size_t seed) noexcept
Seed the generator.
Definition: Random.h:57
auto Between(float min, float max) noexcept -> float
Generate a random float in the range [min, max].
Definition: Random.h:101
Random(size_t seed) noexcept
Construct a new Random object from a seed.
Definition: Random.h:39
auto Between(size_t min, size_t max) noexcept -> size_t
Generate a random size_t in the range [min, max].
Definition: Random.h:109
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