Math.cpp
Go to the documentation of this file.
#include "Math.h"
#include "Constants.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 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;
}
std::mt19937 Math::rng;
bool Math::isRngInitialized = true;
MAKE_SURE_RNG_IS_INITIALIZED
#define MAKE_SURE_RNG_IS_INITIALIZED
Definition: Math.cpp:8
Eule::Math::RandomChance
static bool RandomChance(const double chance)
Will 'roll' a dice, returning true percent of the time.
Definition: Math.cpp:73
PI
static constexpr double PI
Pi up to 50 decimal places.
Definition: Constants.h:6
Eule::Math::RandomRange
static double RandomRange(const double min, const double max)
Will return a random double within a range These bounds are INCLUSIVE!
Definition: Math.cpp:56
Constants.h
Eule::Math::RandomInt
static unsigned int RandomInt()
Will return a random integer.
Definition: Math.cpp:45
Math.h
Eule::Math::RandomIntRange
static int RandomIntRange(const int max, const int min)
Will return a random integer within a range.
Definition: Math.cpp:63
Eule::Math::Random
static double Random()
Will return a random double between 0 and 1
Definition: Math.cpp:29
Eule::Math::RandomUint
static unsigned int RandomUint()
Will return a random unsigned integer.
Definition: Math.cpp:37
HALF_PI
static constexpr double HALF_PI
Pi divided by two.
Definition: Constants.h:9
Eule
Definition: Collider.h:4
Eule::Math::Oscillate
static double Oscillate(const double a, const double b, const double counter, const double speed)
Kind of like , but it oscillates over instead of , by a given speed.
Definition: Math.cpp:68