Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Math.cpp
Go to the documentation of this file.
1 #include "Math.h"
2 #include "Constants.h"
3 #include <array>
4 
5 using namespace Eule;
6 
7 // Checks if the random number generator is initialized. Does nothing if it is, initializes if it isn't.
8 #define MAKE_SURE_RNG_IS_INITIALIZED if (!isRngInitialized) InitRng();
9 
10 void Math::InitRng()
11 {
12  // Create truly random source (from hardware events)
13  std::random_device randomSource;
14 
15  // Generate enough truly random values to populate the entire state of the mersenne twister
16  std::array<int, std::mt19937::state_size> seedValues;
17  std::generate_n(seedValues.data(), seedValues.size(), std::ref(randomSource));
18  std::seed_seq seedSequence(seedValues.begin(), seedValues.end());
19 
20  // Seed the mersenne twister with these values
21  rng = std::mt19937(seedSequence);
22 
23  isRngInitialized = true;
24 
25  return;
26 }
27 
28 // Will return a random double between 0 and 1
29 double Math::Random()
30 {
32 
33  return (rng() % 694206942069ll) / 694206942069.0;
34 }
35 
36 // Will return a random unsigned integer.
37 unsigned int Math::RandomUint()
38 {
40 
41  return rng();
42 }
43 
44 // Will return a random integer
45 unsigned int Math::RandomInt()
46 {
48 
49  // Since this is supposed to return a random value anyways,
50  // we can let the random uint overflow without any problems.
51  return (int)rng();
52 }
53 
54 // Will return a random double within a range
55 // These bounds are INCLUSIVE!
56 double Math::RandomRange(double min, double max)
57 {
58  return (Random() * (max - min)) + min;
59 }
60 
61 // Will return a random integer within a range. This is faster than '(int)RandomRange(x,y)'
62 // These bounds are INCLUSIVE!
63 int Math::RandomIntRange(int min, int max)
64 {
65  return (rng() % (max + 1 - min)) + min;
66 }
67 
68 double Math::Oscillate(const double a, const double b, const double counter, const double speed)
69 {
70  return (sin(counter * speed * PI - HALF_PI) * 0.5 + 0.5) * (b-a) + a;
71 }
72 
73 bool Math::RandomChance(const double chance)
74 {
75  return Random() <= chance;
76 }
77 
78 int Math::Mod(const int numerator, const int denominator)
79 {
80  if (denominator == 0)
81  throw std::logic_error("Divide by zero");
82 
83  // Quick optimizations:
84 
85  // -> 0/n is always 0
86  if (numerator == 0)
87  return 0;
88 
89  // -> operator% works for a > 0 && b > 0
90  if (denominator > 0 && numerator > 0)
91  return numerator % denominator;
92 
93  // Else: generalized formula
94  return (denominator + (numerator % denominator)) % denominator;
95 }
96 
97 std::mt19937 Math::rng;
98 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
Eule::Math::Mod
static int Mod(const int numerator, const int denominator)
Will compute the actual modulo of a fraction.
Definition: Math.cpp:78
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