Plasma Engine  2.0
Loading...
Searching...
No Matches
Random.h
1#pragma once
2
3#include <Foundation/Containers/DynamicArray.h>
4#include <Foundation/IO/Stream.h>
5#include <Foundation/Math/Declarations.h>
6
8class PL_FOUNDATION_DLL plRandom
9{
10public:
11 plRandom();
12
14 void Initialize(plUInt64 uiSeed); // [tested]
15
18 void InitializeFromCurrentTime();
19
21 void Save(plStreamWriter& inout_stream) const; // [tested]
22
24 void Load(plStreamReader& inout_stream); // [tested]
25
27 plUInt32 UInt(); // [tested]
28
33 plUInt32 UIntInRange(plUInt32 uiRange); // [tested]
34
39 plInt32 IntInRange(plInt32 iMinValue, plUInt32 uiRange); // [tested]
40
42 plInt32 IntMinMax(plInt32 iMinValue, plInt32 iMaxValue); // [tested]
43
45 PL_ALWAYS_INLINE bool Bool() { return static_cast<bool>(UInt() & 1); } // [tested]
46
48 PL_ALWAYS_INLINE double DoubleZeroToOneExclusive() { return (double)UInt() / (double)(0xFFFFFFFFUL); } // [tested]
49
51 PL_ALWAYS_INLINE double DoubleZeroToOneInclusive() { return (double)UInt() / (double)(0xFFFFFFFFUL + 1.0); } // [tested]
52
54 double DoubleInRange(double fMinValue, double fRange); // [tested]
55
57 double DoubleMinMax(double fMinValue, double fMaxValue); // [tested]
58
60 double DoubleVariance(double fValue, double fVariance);
61
63 double DoubleVarianceAroundZero(double fAbsMaxValue);
64
66 PL_ALWAYS_INLINE float FloatZeroToOneExclusive() { return static_cast<float>(DoubleZeroToOneExclusive()); } // [tested]
67
69 PL_ALWAYS_INLINE float FloatZeroToOneInclusive() { return static_cast<float>(DoubleZeroToOneInclusive()); } // [tested]
70
72 PL_ALWAYS_INLINE float FloatInRange(float fMinValue, float fRange) { return static_cast<float>(DoubleInRange(fMinValue, fRange)); } // [tested]
73
75 PL_ALWAYS_INLINE float FloatMinMax(float fMinValue, float fMaxValue) { return static_cast<float>(DoubleMinMax(fMinValue, fMaxValue)); } // [tested]
76
78 PL_ALWAYS_INLINE float FloatVariance(float fValue, float fVariance) { return static_cast<float>(DoubleVariance(fValue, fVariance)); }
79
81 PL_ALWAYS_INLINE float FloatVarianceAroundZero(float fAbsMaxValue) { return static_cast<float>(DoubleVarianceAroundZero(fAbsMaxValue)); }
82
83private:
84 plUInt32 m_uiIndex;
85 plUInt32 m_uiState[16];
86};
87
88
90class PL_FOUNDATION_DLL plRandomGauss
91{
92public:
99 void Initialize(plUInt64 uiRandomSeed, plUInt32 uiMaxValue, float fVariance = 1.0f); // [tested]
100
102 plUInt32 UnsignedValue(); // [tested]
103
105 plInt32 SignedValue(); // [tested]
106
108 void Save(plStreamWriter& inout_stream) const; // [tested]
109
111 void Load(plStreamReader& inout_stream); // [tested]
112
113private:
114 void SetupTable(plUInt32 uiMaxValue, float fSigma);
115
116 float m_fSigma;
117 double m_fAreaSum;
118 plDynamicArray<float> m_GaussAreaSum;
119 plRandom m_Generator;
120};
121
122#include <Foundation/Math/Implementation/AllClassesRandom_inl.h>
Definition DynamicArray.h:81
A random number generator that produces values with a normal / Gaussian distribution.
Definition Random.h:91
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
PL_ALWAYS_INLINE float FloatMinMax(float fMinValue, float fMaxValue)
Returns a float value in range [fMinValue ; fMaxValue].
Definition Random.h:75
PL_ALWAYS_INLINE bool Bool()
Returns a boolean either being true or false.
Definition Random.h:45
PL_ALWAYS_INLINE float FloatInRange(float fMinValue, float fRange)
Returns a float value in range [fMinValue ; fMinValue + fRange)
Definition Random.h:72
PL_ALWAYS_INLINE float FloatVariance(float fValue, float fVariance)
Returns a float value around fValue with a given variance (0 - 1 range)
Definition Random.h:78
PL_ALWAYS_INLINE float FloatVarianceAroundZero(float fAbsMaxValue)
Returns a float value between [-fAbsMaxValue; +fAbsMaxValue] with a Gaussian distribution.
Definition Random.h:81
PL_ALWAYS_INLINE float FloatZeroToOneExclusive()
Returns a value in range [0.0 ; 1.0), ie. including zero, but excluding one.
Definition Random.h:66
PL_ALWAYS_INLINE double DoubleZeroToOneExclusive()
Returns a value in range [0.0 ; 1.0), ie. including zero, but excluding one.
Definition Random.h:48
PL_ALWAYS_INLINE float FloatZeroToOneInclusive()
Returns a value in range [0.0 ; 1.0], ie. including zero and one.
Definition Random.h:69
PL_ALWAYS_INLINE double DoubleZeroToOneInclusive()
Returns a value in range [0.0 ; 1.0], ie. including zero and one.
Definition Random.h:51
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107