Moved rng-methods to their own class/header
This commit is contained in:
parent
72fc9b3538
commit
1cc7699939
@ -23,6 +23,7 @@
|
|||||||
<ClCompile Include="Math.cpp" />
|
<ClCompile Include="Math.cpp" />
|
||||||
<ClCompile Include="Matrix4x4.cpp" />
|
<ClCompile Include="Matrix4x4.cpp" />
|
||||||
<ClCompile Include="Quaternion.cpp" />
|
<ClCompile Include="Quaternion.cpp" />
|
||||||
|
<ClCompile Include="Random.cpp" />
|
||||||
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
||||||
<ClCompile Include="Vector2.cpp" />
|
<ClCompile Include="Vector2.cpp" />
|
||||||
<ClCompile Include="Vector3.cpp" />
|
<ClCompile Include="Vector3.cpp" />
|
||||||
@ -34,6 +35,7 @@
|
|||||||
<ClInclude Include="Math.h" />
|
<ClInclude Include="Math.h" />
|
||||||
<ClInclude Include="Matrix4x4.h" />
|
<ClInclude Include="Matrix4x4.h" />
|
||||||
<ClInclude Include="Quaternion.h" />
|
<ClInclude Include="Quaternion.h" />
|
||||||
|
<ClInclude Include="Random.h" />
|
||||||
<ClInclude Include="Rect.h" />
|
<ClInclude Include="Rect.h" />
|
||||||
<ClInclude Include="TrapazoidalPrismCollider.h" />
|
<ClInclude Include="TrapazoidalPrismCollider.h" />
|
||||||
<ClInclude Include="Vector2.h" />
|
<ClInclude Include="Vector2.h" />
|
||||||
|
@ -39,6 +39,9 @@
|
|||||||
<ClCompile Include="TrapazoidalPrismCollider.cpp">
|
<ClCompile Include="TrapazoidalPrismCollider.cpp">
|
||||||
<Filter>Quelldateien</Filter>
|
<Filter>Quelldateien</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Random.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Matrix4x4.h">
|
<ClInclude Include="Matrix4x4.h">
|
||||||
@ -74,5 +77,8 @@
|
|||||||
<ClInclude Include="version.h">
|
<ClInclude Include="version.h">
|
||||||
<Filter>Headerdateien</Filter>
|
<Filter>Headerdateien</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Random.h">
|
||||||
|
<Filter>Headerdateien</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -4,77 +4,6 @@
|
|||||||
|
|
||||||
using namespace Eule;
|
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)
|
int Math::Mod(const int numerator, const int denominator)
|
||||||
{
|
{
|
||||||
if (denominator == 0)
|
if (denominator == 0)
|
||||||
@ -94,5 +23,7 @@ int Math::Mod(const int numerator, const int denominator)
|
|||||||
return (denominator + (numerator % denominator)) % denominator;
|
return (denominator + (numerator % denominator)) % denominator;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::mt19937 Math::rng;
|
double Math::Oscillate(const double a, const double b, const double counter, const double speed)
|
||||||
bool Math::isRngInitialized = true;
|
{
|
||||||
|
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
|
#pragma once
|
||||||
#include <random>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace Eule
|
namespace Eule
|
||||||
@ -30,26 +29,6 @@ namespace Eule
|
|||||||
//! Will compute the actual modulo of a fraction. The % operator returns bs for n<0.
|
//! Will compute the actual modulo of a fraction. The % operator returns bs for n<0.
|
||||||
//! May throw division-by-zero std::logic_error
|
//! May throw division-by-zero std::logic_error
|
||||||
[[nodiscard]] static int Mod(const int numerator, const int denominator);
|
[[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.
|
//! 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.
|
//! 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);
|
static double Oscillate(const double a, const double b, const double counter, const double speed);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Will initialize the random number generator
|
|
||||||
static void InitRng();
|
|
||||||
|
|
||||||
static std::mt19937 rng;
|
|
||||||
static bool isRngInitialized;
|
|
||||||
|
|
||||||
// No instanciation! >:(
|
// No instanciation! >:(
|
||||||
Math();
|
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();
|
||||||
|
};
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "../_TestingUtilities/Testutil.h"
|
#include "../_TestingUtilities/Testutil.h"
|
||||||
|
#include "../Eule/Random.h"
|
||||||
#include "../Eule/Math.h"
|
#include "../Eule/Math.h"
|
||||||
#include "../Eule/Constants.h"
|
#include "../Eule/Constants.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
@ -21,7 +22,7 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double rnd = Math::RandomRange(-1000, 1000);
|
const double rnd = Random::RandomRange(-1000, 1000);
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(-1, 1, rnd, 1);
|
const double result = Math::Oscillate(-1, 1, rnd, 1);
|
||||||
@ -41,9 +42,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1000, 1000);
|
const double a = Random::RandomRange(-1000, 1000);
|
||||||
const double b = Math::RandomRange(-1000, 1000);
|
const double b = Random::RandomRange(-1000, 1000);
|
||||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, even, 1);
|
const double result = Math::Oscillate(a, b, even, 1);
|
||||||
@ -70,9 +71,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1000, 1000);
|
const double a = Random::RandomRange(-1000, 1000);
|
||||||
const double b = Math::RandomRange(-1000, 1000);
|
const double b = Random::RandomRange(-1000, 1000);
|
||||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, uneven, 1);
|
const double result = Math::Oscillate(a, b, uneven, 1);
|
||||||
@ -90,9 +91,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1000, 1000);
|
const double a = Random::RandomRange(-1000, 1000);
|
||||||
const double b = Math::RandomRange(-1000, 1000);
|
const double b = Random::RandomRange(-1000, 1000);
|
||||||
const int anInt = Math::RandomIntRange(-1000, 1000);
|
const int anInt = Random::RandomIntRange(-1000, 1000);
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, anInt + 0.5, 1);
|
const double result = Math::Oscillate(a, b, anInt + 0.5, 1);
|
||||||
@ -118,9 +119,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1, 1);
|
const double a = Random::RandomRange(-1, 1);
|
||||||
const double b = Math::RandomRange(-1, 1);
|
const double b = Random::RandomRange(-1, 1);
|
||||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, even + 0.25, 1);
|
const double result = Math::Oscillate(a, b, even + 0.25, 1);
|
||||||
@ -148,9 +149,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1, 1);
|
const double a = Random::RandomRange(-1, 1);
|
||||||
const double b = Math::RandomRange(-1, 1);
|
const double b = Random::RandomRange(-1, 1);
|
||||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, even + 0.75, 1);
|
const double result = Math::Oscillate(a, b, even + 0.75, 1);
|
||||||
@ -170,9 +171,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1, 1);
|
const double a = Random::RandomRange(-1, 1);
|
||||||
const double b = Math::RandomRange(-1, 1);
|
const double b = Random::RandomRange(-1, 1);
|
||||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, uneven + 0.25, 1);
|
const double result = Math::Oscillate(a, b, uneven + 0.25, 1);
|
||||||
@ -192,9 +193,9 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1, 1);
|
const double a = Random::RandomRange(-1, 1);
|
||||||
const double b = Math::RandomRange(-1, 1);
|
const double b = Random::RandomRange(-1, 1);
|
||||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, uneven + 0.75, 1);
|
const double result = Math::Oscillate(a, b, uneven + 0.75, 1);
|
||||||
@ -214,8 +215,8 @@ namespace _Math
|
|||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
const double a = Math::RandomRange(-1000, 1000);
|
const double a = Random::RandomRange(-1000, 1000);
|
||||||
const double b = Math::RandomRange(-1000, 1000);
|
const double b = Random::RandomRange(-1000, 1000);
|
||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
const double result = Math::Oscillate(a, b, 0.5, 2);
|
const double result = Math::Oscillate(a, b, 0.5, 2);
|
||||||
|
1
Test/Random.cpp
Normal file
1
Test/Random.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "Random.h"
|
41
Test/Random.h
Normal file
41
Test/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 Rand();
|
||||||
|
|
||||||
|
//! 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();
|
||||||
|
};
|
||||||
|
}
|
@ -1,13 +1,13 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "../_TestingUtilities/Testutil.h"
|
#include "../_TestingUtilities/Testutil.h"
|
||||||
#include "../Eule/Math.h"
|
#include "../Eule/Random.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
using namespace Eule;
|
using namespace Eule;
|
||||||
|
|
||||||
namespace _Math
|
namespace _Random
|
||||||
{
|
{
|
||||||
TEST_CLASS(_RandomIntRange)
|
TEST_CLASS(_RandomIntRange)
|
||||||
{
|
{
|
||||||
@ -18,7 +18,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
int rnd = Math::RandomIntRange(49, 99);
|
int rnd = Random::RandomIntRange(49, 99);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= 49, L"rnd too small");
|
Assert::IsTrue(rnd >= 49, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= 99, L"rnd too big");
|
Assert::IsTrue(rnd <= 99, L"rnd too big");
|
||||||
@ -33,7 +33,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
int rnd = Math::RandomIntRange(-39, 99);
|
int rnd = Random::RandomIntRange(-39, 99);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= -39, L"rnd too small");
|
Assert::IsTrue(rnd >= -39, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= 99, L"rnd too big");
|
Assert::IsTrue(rnd <= 99, L"rnd too big");
|
||||||
@ -48,7 +48,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
int rnd = Math::RandomIntRange(-39, -10);
|
int rnd = Random::RandomIntRange(-39, -10);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= -39, L"rnd too small");
|
Assert::IsTrue(rnd >= -39, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= -10, L"rnd too big");
|
Assert::IsTrue(rnd <= -10, L"rnd too big");
|
||||||
@ -68,7 +68,7 @@ namespace _Math
|
|||||||
|
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
int randomVal = Math::RandomIntRange(0, 9);
|
int randomVal = Random::RandomIntRange(0, 9);
|
||||||
foundDigits[randomVal] = true;
|
foundDigits[randomVal] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ namespace _Math
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Create 1000 random values
|
// Create 1000 random values
|
||||||
std::generate_n(rands.data(), rands.size(), []()->int { return Math::RandomIntRange(100, (int)4e9); });
|
std::generate_n(rands.data(), rands.size(), []()->int { return Random::RandomIntRange(100, (int)4e9); });
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
const double stddev = Testutil::Stddev(rands);
|
const double stddev = Testutil::Stddev(rands);
|
@ -1,12 +1,12 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "../Eule/Math.h"
|
#include "../Eule/Random.h"
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
using namespace Eule;
|
using namespace Eule;
|
||||||
|
|
||||||
namespace _Math
|
namespace _Random
|
||||||
{
|
{
|
||||||
TEST_CLASS(_Random)
|
TEST_CLASS(_RandomFloat)
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Checks that all values are always 0 <= v <= 1
|
// Checks that all values are always 0 <= v <= 1
|
||||||
@ -15,7 +15,7 @@ namespace _Math
|
|||||||
// Test 1000 random values
|
// Test 1000 random values
|
||||||
for (std::size_t i = 0; i < 1e3; i++)
|
for (std::size_t i = 0; i < 1e3; i++)
|
||||||
{
|
{
|
||||||
const double rnd = Math::Random();
|
const double rnd = Random::RandomFloat();
|
||||||
Assert::IsTrue(rnd >= 0.0, L"rnd < 0");
|
Assert::IsTrue(rnd >= 0.0, L"rnd < 0");
|
||||||
Assert::IsTrue(rnd <= 1.0, L"rnd > 1");
|
Assert::IsTrue(rnd <= 1.0, L"rnd > 1");
|
||||||
}
|
}
|
@ -1,13 +1,13 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "../_TestingUtilities/Testutil.h"
|
#include "../_TestingUtilities/Testutil.h"
|
||||||
#include "../Eule/Math.h"
|
#include "../Eule/Random.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
using namespace Eule;
|
using namespace Eule;
|
||||||
|
|
||||||
namespace _Math
|
namespace _Random
|
||||||
{
|
{
|
||||||
TEST_CLASS(_RandomInteger)
|
TEST_CLASS(_RandomInteger)
|
||||||
{
|
{
|
||||||
@ -21,7 +21,7 @@ namespace _Math
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Create 1000 random values
|
// Create 1000 random values
|
||||||
std::generate_n(rands.data(), rands.size(), []()->unsigned int { return Math::RandomUint(); });
|
std::generate_n(rands.data(), rands.size(), []()->unsigned int { return Random::RandomUint(); });
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
const double stddev = Testutil::Stddev<unsigned int>(rands);
|
const double stddev = Testutil::Stddev<unsigned int>(rands);
|
||||||
@ -39,7 +39,7 @@ namespace _Math
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Create 1000 random values
|
// Create 1000 random values
|
||||||
std::generate_n(rands.data(), rands.size(), []()->int { return Math::RandomInt(); });
|
std::generate_n(rands.data(), rands.size(), []()->int { return Random::RandomInt(); });
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
const double stddev = Testutil::Stddev<int>(rands);
|
const double stddev = Testutil::Stddev<int>(rands);
|
@ -1,13 +1,13 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "../_TestingUtilities/Testutil.h"
|
#include "../_TestingUtilities/Testutil.h"
|
||||||
#include "../Eule/Math.h"
|
#include "../Eule/Random.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
using namespace Eule;
|
using namespace Eule;
|
||||||
|
|
||||||
namespace _Math
|
namespace _Random
|
||||||
{
|
{
|
||||||
TEST_CLASS(_RandomRange)
|
TEST_CLASS(_RandomRange)
|
||||||
{
|
{
|
||||||
@ -19,7 +19,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
double rnd = Math::RandomRange(49.0, 99.0);
|
double rnd = Random::RandomRange(49.0, 99.0);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= 49.0, L"rnd too small");
|
Assert::IsTrue(rnd >= 49.0, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
|
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
|
||||||
@ -34,7 +34,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
double rnd = Math::RandomRange(-39.0, 99.0);
|
double rnd = Random::RandomRange(-39.0, 99.0);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
|
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
|
Assert::IsTrue(rnd <= 99.0, L"rnd too big");
|
||||||
@ -49,7 +49,7 @@ namespace _Math
|
|||||||
// Test 1000 random integers
|
// Test 1000 random integers
|
||||||
for (std::size_t i = 0; i < 1000; i++)
|
for (std::size_t i = 0; i < 1000; i++)
|
||||||
{
|
{
|
||||||
double rnd = Math::RandomRange(-39.0, -10.0);
|
double rnd = Random::RandomRange(-39.0, -10.0);
|
||||||
|
|
||||||
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
|
Assert::IsTrue(rnd >= -39.0, L"rnd too small");
|
||||||
Assert::IsTrue(rnd <= -10.0, L"rnd too big");
|
Assert::IsTrue(rnd <= -10.0, L"rnd too big");
|
||||||
@ -67,7 +67,7 @@ namespace _Math
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Create 1000 random values
|
// Create 1000 random values
|
||||||
std::generate_n(rands.data(), rands.size(), []()->double { return Math::RandomRange(100, 4e9); });
|
std::generate_n(rands.data(), rands.size(), []()->double { return Random::RandomRange(100, 4e9); });
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
const double stddev = Testutil::Stddev(rands);
|
const double stddev = Testutil::Stddev(rands);
|
@ -29,13 +29,13 @@
|
|||||||
<ClCompile Include="Math_Lerp.cpp" />
|
<ClCompile Include="Math_Lerp.cpp" />
|
||||||
<ClCompile Include="Math_Max.cpp" />
|
<ClCompile Include="Math_Max.cpp" />
|
||||||
<ClCompile Include="Math_Min.cpp" />
|
<ClCompile Include="Math_Min.cpp" />
|
||||||
<ClCompile Include="Math_Random.cpp" />
|
<ClCompile Include="Random__RandomFloat.cpp" />
|
||||||
<ClCompile Include="Math_RandomInteger.cpp" />
|
<ClCompile Include="Random__RandomInteger.cpp" />
|
||||||
<ClCompile Include="Math_RandomIntRange.cpp" />
|
<ClCompile Include="Random_RandomIntRange.cpp" />
|
||||||
<ClCompile Include="Math_Similar.cpp" />
|
<ClCompile Include="Math_Similar.cpp" />
|
||||||
<ClCompile Include="Math__Mod.cpp" />
|
<ClCompile Include="Math__Mod.cpp" />
|
||||||
<ClCompile Include="Math__Oscillate.cpp" />
|
<ClCompile Include="Math__Oscillate.cpp" />
|
||||||
<ClCompile Include="Math__RandomRange.cpp" />
|
<ClCompile Include="Random__RandomRange.cpp" />
|
||||||
<ClCompile Include="Matrix4x4.cpp" />
|
<ClCompile Include="Matrix4x4.cpp" />
|
||||||
<ClCompile Include="Quaternion.cpp" />
|
<ClCompile Include="Quaternion.cpp" />
|
||||||
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
||||||
|
@ -19,14 +19,14 @@
|
|||||||
<Filter Include="Tests\Math">
|
<Filter Include="Tests\Math">
|
||||||
<UniqueIdentifier>{4af509be-a959-45e0-bec2-03894a16ad60}</UniqueIdentifier>
|
<UniqueIdentifier>{4af509be-a959-45e0-bec2-03894a16ad60}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Tests\Random">
|
||||||
|
<UniqueIdentifier>{b0dcb516-80a3-4bf7-99d7-50186d18e1ea}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Math__Oscillate.cpp">
|
<ClCompile Include="Math__Oscillate.cpp">
|
||||||
<Filter>Tests\Math</Filter>
|
<Filter>Tests\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Math__RandomRange.cpp">
|
|
||||||
<Filter>Tests\Math</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Math_Abs.cpp">
|
<ClCompile Include="Math_Abs.cpp">
|
||||||
<Filter>Tests\Math</Filter>
|
<Filter>Tests\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -42,15 +42,6 @@
|
|||||||
<ClCompile Include="Math_Min.cpp">
|
<ClCompile Include="Math_Min.cpp">
|
||||||
<Filter>Tests\Math</Filter>
|
<Filter>Tests\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Math_Random.cpp">
|
|
||||||
<Filter>Tests\Math</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Math_RandomInteger.cpp">
|
|
||||||
<Filter>Tests\Math</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Math_RandomIntRange.cpp">
|
|
||||||
<Filter>Tests\Math</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Math_Similar.cpp">
|
<ClCompile Include="Math_Similar.cpp">
|
||||||
<Filter>Tests\Math</Filter>
|
<Filter>Tests\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -78,5 +69,17 @@
|
|||||||
<ClCompile Include="Math__Mod.cpp">
|
<ClCompile Include="Math__Mod.cpp">
|
||||||
<Filter>Tests\Math</Filter>
|
<Filter>Tests\Math</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Random__RandomRange.cpp">
|
||||||
|
<Filter>Tests\Random</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Random_RandomIntRange.cpp">
|
||||||
|
<Filter>Tests\Random</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Random__RandomInteger.cpp">
|
||||||
|
<Filter>Tests\Random</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Random__RandomFloat.cpp">
|
||||||
|
<Filter>Tests\Random</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user