Moved rng-methods to their own class/header
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../_TestingUtilities/Testutil.h"
|
||||
#include "../Eule/Random.h"
|
||||
#include "../Eule/Math.h"
|
||||
#include "../Eule/Constants.h"
|
||||
#include <array>
|
||||
@@ -21,7 +22,7 @@ namespace _Math
|
||||
for (std::size_t i = 0; i < 1000; i++)
|
||||
{
|
||||
// Setup
|
||||
const double rnd = Math::RandomRange(-1000, 1000);
|
||||
const double rnd = Random::RandomRange(-1000, 1000);
|
||||
|
||||
// Exercise
|
||||
const double result = Math::Oscillate(-1, 1, rnd, 1);
|
||||
@@ -41,9 +42,9 @@ namespace _Math
|
||||
for (std::size_t i = 0; i < 1000; i++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1000, 1000);
|
||||
const double b = Math::RandomRange(-1000, 1000);
|
||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
||||
const double a = Random::RandomRange(-1000, 1000);
|
||||
const double b = Random::RandomRange(-1000, 1000);
|
||||
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||
|
||||
// Exercise
|
||||
const double result = Math::Oscillate(a, b, even, 1);
|
||||
@@ -70,9 +71,9 @@ namespace _Math
|
||||
for (std::size_t i = 0; i < 1000; i++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1000, 1000);
|
||||
const double b = Math::RandomRange(-1000, 1000);
|
||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
||||
const double a = Random::RandomRange(-1000, 1000);
|
||||
const double b = Random::RandomRange(-1000, 1000);
|
||||
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||
|
||||
// Exercise
|
||||
const double result = Math::Oscillate(a, b, uneven, 1);
|
||||
@@ -90,9 +91,9 @@ namespace _Math
|
||||
for (std::size_t i = 0; i < 1000; i++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1000, 1000);
|
||||
const double b = Math::RandomRange(-1000, 1000);
|
||||
const int anInt = Math::RandomIntRange(-1000, 1000);
|
||||
const double a = Random::RandomRange(-1000, 1000);
|
||||
const double b = Random::RandomRange(-1000, 1000);
|
||||
const int anInt = Random::RandomIntRange(-1000, 1000);
|
||||
|
||||
// Exercise
|
||||
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++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1, 1);
|
||||
const double b = Math::RandomRange(-1, 1);
|
||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
||||
const double a = Random::RandomRange(-1, 1);
|
||||
const double b = Random::RandomRange(-1, 1);
|
||||
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||
|
||||
// Exercise
|
||||
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++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1, 1);
|
||||
const double b = Math::RandomRange(-1, 1);
|
||||
const int even = Math::RandomIntRange(-1000, 1000) & ~1;
|
||||
const double a = Random::RandomRange(-1, 1);
|
||||
const double b = Random::RandomRange(-1, 1);
|
||||
const int even = Random::RandomIntRange(-1000, 1000) & ~1;
|
||||
|
||||
// Exercise
|
||||
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++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1, 1);
|
||||
const double b = Math::RandomRange(-1, 1);
|
||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
||||
const double a = Random::RandomRange(-1, 1);
|
||||
const double b = Random::RandomRange(-1, 1);
|
||||
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||
|
||||
// Exercise
|
||||
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++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1, 1);
|
||||
const double b = Math::RandomRange(-1, 1);
|
||||
const int uneven = Math::RandomIntRange(-1000, 1000) | 1;
|
||||
const double a = Random::RandomRange(-1, 1);
|
||||
const double b = Random::RandomRange(-1, 1);
|
||||
const int uneven = Random::RandomIntRange(-1000, 1000) | 1;
|
||||
|
||||
// Exercise
|
||||
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++)
|
||||
{
|
||||
// Setup
|
||||
const double a = Math::RandomRange(-1000, 1000);
|
||||
const double b = Math::RandomRange(-1000, 1000);
|
||||
const double a = Random::RandomRange(-1000, 1000);
|
||||
const double b = Random::RandomRange(-1000, 1000);
|
||||
|
||||
// Exercise
|
||||
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 "../_TestingUtilities/Testutil.h"
|
||||
#include "../Eule/Math.h"
|
||||
#include "../Eule/Random.h"
|
||||
#include <array>
|
||||
#include <sstream>
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace Eule;
|
||||
|
||||
namespace _Math
|
||||
namespace _Random
|
||||
{
|
||||
TEST_CLASS(_RandomIntRange)
|
||||
{
|
||||
@@ -18,7 +18,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= 99, L"rnd too big");
|
||||
@@ -33,7 +33,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= 99, L"rnd too big");
|
||||
@@ -48,7 +48,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= -10, L"rnd too big");
|
||||
@@ -68,7 +68,7 @@ namespace _Math
|
||||
|
||||
for (std::size_t i = 0; i < 1000; i++)
|
||||
{
|
||||
int randomVal = Math::RandomIntRange(0, 9);
|
||||
int randomVal = Random::RandomIntRange(0, 9);
|
||||
foundDigits[randomVal] = true;
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace _Math
|
||||
|
||||
// Exercise
|
||||
// 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
|
||||
const double stddev = Testutil::Stddev(rands);
|
@@ -1,12 +1,12 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../Eule/Math.h"
|
||||
#include "../Eule/Random.h"
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace Eule;
|
||||
|
||||
namespace _Math
|
||||
namespace _Random
|
||||
{
|
||||
TEST_CLASS(_Random)
|
||||
TEST_CLASS(_RandomFloat)
|
||||
{
|
||||
public:
|
||||
// Checks that all values are always 0 <= v <= 1
|
||||
@@ -15,7 +15,7 @@ namespace _Math
|
||||
// Test 1000 random values
|
||||
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 <= 1.0, L"rnd > 1");
|
||||
}
|
@@ -1,13 +1,13 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../_TestingUtilities/Testutil.h"
|
||||
#include "../Eule/Math.h"
|
||||
#include "../Eule/Random.h"
|
||||
#include <array>
|
||||
#include <sstream>
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace Eule;
|
||||
|
||||
namespace _Math
|
||||
namespace _Random
|
||||
{
|
||||
TEST_CLASS(_RandomInteger)
|
||||
{
|
||||
@@ -21,7 +21,7 @@ namespace _Math
|
||||
|
||||
// Exercise
|
||||
// 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
|
||||
const double stddev = Testutil::Stddev<unsigned int>(rands);
|
||||
@@ -39,7 +39,7 @@ namespace _Math
|
||||
|
||||
// Exercise
|
||||
// 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
|
||||
const double stddev = Testutil::Stddev<int>(rands);
|
@@ -1,13 +1,13 @@
|
||||
#include "CppUnitTest.h"
|
||||
#include "../_TestingUtilities/Testutil.h"
|
||||
#include "../Eule/Math.h"
|
||||
#include "../Eule/Random.h"
|
||||
#include <array>
|
||||
#include <sstream>
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
using namespace Eule;
|
||||
|
||||
namespace _Math
|
||||
namespace _Random
|
||||
{
|
||||
TEST_CLASS(_RandomRange)
|
||||
{
|
||||
@@ -19,7 +19,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= 99.0, L"rnd too big");
|
||||
@@ -34,7 +34,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= 99.0, L"rnd too big");
|
||||
@@ -49,7 +49,7 @@ namespace _Math
|
||||
// Test 1000 random integers
|
||||
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 <= -10.0, L"rnd too big");
|
||||
@@ -67,7 +67,7 @@ namespace _Math
|
||||
|
||||
// Exercise
|
||||
// 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
|
||||
const double stddev = Testutil::Stddev(rands);
|
@@ -29,13 +29,13 @@
|
||||
<ClCompile Include="Math_Lerp.cpp" />
|
||||
<ClCompile Include="Math_Max.cpp" />
|
||||
<ClCompile Include="Math_Min.cpp" />
|
||||
<ClCompile Include="Math_Random.cpp" />
|
||||
<ClCompile Include="Math_RandomInteger.cpp" />
|
||||
<ClCompile Include="Math_RandomIntRange.cpp" />
|
||||
<ClCompile Include="Random__RandomFloat.cpp" />
|
||||
<ClCompile Include="Random__RandomInteger.cpp" />
|
||||
<ClCompile Include="Random_RandomIntRange.cpp" />
|
||||
<ClCompile Include="Math_Similar.cpp" />
|
||||
<ClCompile Include="Math__Mod.cpp" />
|
||||
<ClCompile Include="Math__Oscillate.cpp" />
|
||||
<ClCompile Include="Math__RandomRange.cpp" />
|
||||
<ClCompile Include="Random__RandomRange.cpp" />
|
||||
<ClCompile Include="Matrix4x4.cpp" />
|
||||
<ClCompile Include="Quaternion.cpp" />
|
||||
<ClCompile Include="TrapazoidalPrismCollider.cpp" />
|
||||
|
@@ -19,14 +19,14 @@
|
||||
<Filter Include="Tests\Math">
|
||||
<UniqueIdentifier>{4af509be-a959-45e0-bec2-03894a16ad60}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Tests\Random">
|
||||
<UniqueIdentifier>{b0dcb516-80a3-4bf7-99d7-50186d18e1ea}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Math__Oscillate.cpp">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Math__RandomRange.cpp">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Math_Abs.cpp">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</ClCompile>
|
||||
@@ -42,15 +42,6 @@
|
||||
<ClCompile Include="Math_Min.cpp">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</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">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</ClCompile>
|
||||
@@ -78,5 +69,17 @@
|
||||
<ClCompile Include="Math__Mod.cpp">
|
||||
<Filter>Tests\Math</Filter>
|
||||
</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>
|
||||
</Project>
|
Reference in New Issue
Block a user