Moved rng-methods to their own class/header
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
<ClCompile Include="Math.cpp" />
|
||||
<ClCompile Include="Matrix4x4.cpp" />
|
||||
<ClCompile Include="Quaternion.cpp" />
|
||||
<ClCompile Include="Random.cpp" />
|
||||
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
||||
<ClCompile Include="Vector2.cpp" />
|
||||
<ClCompile Include="Vector3.cpp" />
|
||||
@@ -34,6 +35,7 @@
|
||||
<ClInclude Include="Math.h" />
|
||||
<ClInclude Include="Matrix4x4.h" />
|
||||
<ClInclude Include="Quaternion.h" />
|
||||
<ClInclude Include="Random.h" />
|
||||
<ClInclude Include="Rect.h" />
|
||||
<ClInclude Include="TrapazoidalPrismCollider.h" />
|
||||
<ClInclude Include="Vector2.h" />
|
||||
|
@@ -39,6 +39,9 @@
|
||||
<ClCompile Include="TrapazoidalPrismCollider.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Random.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Matrix4x4.h">
|
||||
@@ -74,5 +77,8 @@
|
||||
<ClInclude Include="version.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Random.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@@ -4,77 +4,6 @@
|
||||
|
||||
using namespace Eule;
|
||||
|
||||
// Checks if the random number generator is initialized. Does nothing if it is, initializes if it isn't.
|
||||
#define MAKE_SURE_RNG_IS_INITIALIZED if (!isRngInitialized) InitRng();
|
||||
|
||||
void Math::InitRng()
|
||||
{
|
||||
// Create truly random source (from hardware events)
|
||||
std::random_device randomSource;
|
||||
|
||||
// Generate enough truly random values to populate the entire state of the mersenne twister
|
||||
std::array<int, std::mt19937::state_size> seedValues;
|
||||
std::generate_n(seedValues.data(), seedValues.size(), std::ref(randomSource));
|
||||
std::seed_seq seedSequence(seedValues.begin(), seedValues.end());
|
||||
|
||||
// Seed the mersenne twister with these values
|
||||
rng = std::mt19937(seedSequence);
|
||||
|
||||
isRngInitialized = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Will return a random double between 0 and 1
|
||||
double Math::Random()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
return (rng() % 694206942069ll) / 694206942069.0;
|
||||
}
|
||||
|
||||
// Will return a random unsigned integer.
|
||||
unsigned int Math::RandomUint()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
return rng();
|
||||
}
|
||||
|
||||
// Will return a random integer
|
||||
unsigned int Math::RandomInt()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
// Since this is supposed to return a random value anyways,
|
||||
// we can let the random uint overflow without any problems.
|
||||
return (int)rng();
|
||||
}
|
||||
|
||||
// Will return a random double within a range
|
||||
// These bounds are INCLUSIVE!
|
||||
double Math::RandomRange(double min, double max)
|
||||
{
|
||||
return (Random() * (max - min)) + min;
|
||||
}
|
||||
|
||||
// Will return a random integer within a range. This is faster than '(int)RandomRange(x,y)'
|
||||
// These bounds are INCLUSIVE!
|
||||
int Math::RandomIntRange(int min, int max)
|
||||
{
|
||||
return (rng() % (max + 1 - min)) + min;
|
||||
}
|
||||
|
||||
double Math::Oscillate(const double a, const double b, const double counter, const double speed)
|
||||
{
|
||||
return (sin(counter * speed * PI - HALF_PI) * 0.5 + 0.5) * (b-a) + a;
|
||||
}
|
||||
|
||||
bool Math::RandomChance(const double chance)
|
||||
{
|
||||
return Random() <= chance;
|
||||
}
|
||||
|
||||
int Math::Mod(const int numerator, const int denominator)
|
||||
{
|
||||
if (denominator == 0)
|
||||
@@ -94,5 +23,7 @@ int Math::Mod(const int numerator, const int denominator)
|
||||
return (denominator + (numerator % denominator)) % denominator;
|
||||
}
|
||||
|
||||
std::mt19937 Math::rng;
|
||||
bool Math::isRngInitialized = true;
|
||||
double Math::Oscillate(const double a, const double b, const double counter, const double speed)
|
||||
{
|
||||
return (sin(counter * speed * PI - HALF_PI) * 0.5 + 0.5) * (b - a) + a;
|
||||
}
|
||||
|
27
Eule/Math.h
27
Eule/Math.h
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Eule
|
||||
@@ -30,26 +29,6 @@ namespace Eule
|
||||
//! Will compute the actual modulo of a fraction. The % operator returns bs for n<0.
|
||||
//! May throw division-by-zero std::logic_error
|
||||
[[nodiscard]] static int Mod(const int numerator, const int denominator);
|
||||
|
||||
//! Will return a random double between `0` and `1`
|
||||
static double Random();
|
||||
|
||||
//! Will return a random unsigned integer.
|
||||
static unsigned int RandomUint();
|
||||
|
||||
//! Will return a random integer
|
||||
static unsigned int RandomInt();
|
||||
|
||||
//! Will return a random double within a range
|
||||
//! These bounds are INCLUSIVE!
|
||||
static double RandomRange(const double min, const double max);
|
||||
|
||||
//! Will return a random integer within a range. This is faster than `(int)RandomRange(x,y)`
|
||||
//! These bounds are INCLUSIVE!
|
||||
static int RandomIntRange(const int max, const int min);
|
||||
|
||||
//! Will 'roll' a dice, returning `true` \f$100 * chance\f$ percent of the time.
|
||||
static bool RandomChance(const double chance);
|
||||
|
||||
//! Kind of like \f$sin(counter)\f$, but it oscillates over \f$[a,b]\f$ instead of \f$[-1,1]\f$, by a given speed.
|
||||
//! Given that \f$speed = 1\f$, the result will always be `a` if `counter` is even, and `b` if `counter` is uneven.
|
||||
@@ -58,12 +37,6 @@ namespace Eule
|
||||
static double Oscillate(const double a, const double b, const double counter, const double speed);
|
||||
|
||||
private:
|
||||
//! Will initialize the random number generator
|
||||
static void InitRng();
|
||||
|
||||
static std::mt19937 rng;
|
||||
static bool isRngInitialized;
|
||||
|
||||
// No instanciation! >:(
|
||||
Math();
|
||||
};
|
||||
|
73
Eule/Random.cpp
Normal file
73
Eule/Random.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "Random.h"
|
||||
#include <Array>
|
||||
|
||||
using namespace Eule;
|
||||
|
||||
// Checks if the random number generator is initialized. Does nothing if it is, initializes if it isn't.
|
||||
#define MAKE_SURE_RNG_IS_INITIALIZED if (!isRngInitialized) InitRng();
|
||||
|
||||
void Random::InitRng()
|
||||
{
|
||||
// Create truly random source (from hardware events)
|
||||
std::random_device randomSource;
|
||||
|
||||
// Generate enough truly random values to populate the entire state of the mersenne twister
|
||||
std::array<int, std::mt19937::state_size> seedValues;
|
||||
std::generate_n(seedValues.data(), seedValues.size(), std::ref(randomSource));
|
||||
std::seed_seq seedSequence(seedValues.begin(), seedValues.end());
|
||||
|
||||
// Seed the mersenne twister with these values
|
||||
rng = std::mt19937(seedSequence);
|
||||
|
||||
isRngInitialized = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Will return a random double between 0 and 1
|
||||
double Random::RandomFloat()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
return (rng() % 694206942069ll) / 694206942069.0;
|
||||
}
|
||||
|
||||
// Will return a random unsigned integer.
|
||||
unsigned int Random::RandomUint()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
return rng();
|
||||
}
|
||||
|
||||
// Will return a random integer
|
||||
unsigned int Random::RandomInt()
|
||||
{
|
||||
MAKE_SURE_RNG_IS_INITIALIZED;
|
||||
|
||||
// Since this is supposed to return a random value anyways,
|
||||
// we can let the random uint overflow without any problems.
|
||||
return (int)rng();
|
||||
}
|
||||
|
||||
// Will return a random double within a range
|
||||
// These bounds are INCLUSIVE!
|
||||
double Random::RandomRange(double min, double max)
|
||||
{
|
||||
return (RandomFloat() * (max - min)) + min;
|
||||
}
|
||||
|
||||
// Will return a random integer within a range. This is faster than '(int)RandomRange(x,y)'
|
||||
// These bounds are INCLUSIVE!
|
||||
int Random::RandomIntRange(int min, int max)
|
||||
{
|
||||
return (rng() % (max + 1 - min)) + min;
|
||||
}
|
||||
|
||||
bool Random::RandomChance(const double chance)
|
||||
{
|
||||
return RandomFloat() <= chance;
|
||||
}
|
||||
|
||||
std::mt19937 Random::rng;
|
||||
bool Random::isRngInitialized = true;
|
41
Eule/Random.h
Normal file
41
Eule/Random.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <random>
|
||||
|
||||
namespace Eule
|
||||
{
|
||||
/** Extensive random number generator
|
||||
*/
|
||||
class Random
|
||||
{
|
||||
public:
|
||||
//! Will return a random double between `0` and `1`
|
||||
static double RandomFloat();
|
||||
|
||||
//! Will return a random unsigned integer.
|
||||
static unsigned int RandomUint();
|
||||
|
||||
//! Will return a random integer
|
||||
static unsigned int RandomInt();
|
||||
|
||||
//! Will return a random double within a range
|
||||
//! These bounds are INCLUSIVE!
|
||||
static double RandomRange(const double min, const double max);
|
||||
|
||||
//! Will return a random integer within a range. This is faster than `(int)RandomRange(x,y)`
|
||||
//! These bounds are INCLUSIVE!
|
||||
static int RandomIntRange(const int max, const int min);
|
||||
|
||||
//! Will 'roll' a dice, returning `true` \f$100 * chance\f$ percent of the time.
|
||||
static bool RandomChance(const double chance);
|
||||
|
||||
private:
|
||||
//! Will initialize the random number generator
|
||||
static void InitRng();
|
||||
|
||||
static std::mt19937 rng;
|
||||
static bool isRngInitialized;
|
||||
|
||||
// No instanciation! >:(
|
||||
Random();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user