Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
Random.cpp
Go to the documentation of this file.
2 #include "Engine/engine.hpp"
3 #include <cstdlib>
4 #include <ctime>
5 
6 namespace Engine
7 {
8  Random::Random() : lastResultInt(0), lastResultFloat(0.0f)
9  {
10  //Use current time as seed
11  srand(unsigned int(time(nullptr)));
12  }
13 
14  int const& Random::GenerateInt(int const& min, int const& max)
15  {
16  //Generate a number with the passed min and max flipped if the passed min is bigger than max
17  if(min > max)
18  {
19  debug_warning("Random", "GenerateInt", "Passed Min value is larger than passed Max, returning GenerateInt() with passed Min as Max and passed Max as Min.");
20  return GenerateInt(max, min);
21  }
22 
23  //Return the passed min if the passed min and max are the same
24  if (min == max)
25  {
26  debug_warning("Random", "GenerateInt", "Min and Max values passed have the same value, returning passed Min value.");
27  return min;
28  }
29 
30  //Generate the result and store it
31  lastResultInt = rand() % (max + 1 - min) + min;
32 
33  //Return the result
34  return lastResultInt;
35  }
36 
37  float const& Random::GenerateFloat(float const& min, float const& max)
38  {
39  //Generate a number with the passed min and max flipped if the passed min is bigger than max
40  if (min > max)
41  {
42  debug_warning("Random", "GenerateFloat", "Passed Min value is larger than passed Max, returning GenerateInt() with passed Min as Max and passed Max as Min.");
43  return GenerateFloat(max, min);
44  }
45 
46  //Return the passed min if the passed min and max are the same
47  if (min == max)
48  {
49  debug_warning("Random", "GenerateFloat", "Min and Max values passed have the same value, returning passed Min value.");
50  return min;
51  }
52 
53  //Generate the result and store it
54  const float shifter = 1000.0f;
55  lastResultFloat = float(rand() % int(floorf((max - min) * shifter))) / shifter + min ;
56 
57  //Return the result
58  return lastResultFloat;
59  }
60 } //namespace Engine
#define debug_warning(debug_class, function, value)
This functions logs a debug warning to the log and console.
Definition: Logging.hpp:45