OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
random.cpp
Go to the documentation of this file.
1 
8 #include <chrono>
9 #include <map>
10 #include <random>
11 #include <utility>
12 
13 #include "global.h"
14 #include "utils/random.h"
15 
16 static std::map<int, std::uniform_int_distribution<int>> distributions;
17 static std::default_random_engine engine;
18 static bool engineIsSeeded = false;
19 
20 int randomInteger(int max, int min) {
21  if (max == min)
22  return max;
23 
24  assertGreaterThan(max, min);
25 
26  if (!engineIsSeeded) {
27  engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
28  engineIsSeeded = true;
29  }
30 
31  int range = max - min;
32  auto elem = distributions.find(range);
33  if (elem == distributions.end()) {
34  distributions[range] = std::uniform_int_distribution<int>(0, range);
35  return distributions[range](engine) + min;
36  } else {
37  return std::get<1>(*elem)(engine) + min;
38  }
39 }
40 
static std::default_random_engine engine
Definition: random.cpp:17
Included everywhere.
#define assertGreaterThan(x, y)
Definition: global.h:162
int randomInteger(int max, int min)
Definition: random.cpp:20
static std::map< int, std::uniform_int_distribution< int > > distributions
Definition: random.cpp:16
static bool engineIsSeeded
Definition: random.cpp:18
Random number generation.