Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Random Class Reference

Random number generator based on Mersenne Twister. More...

#include <Random.h>

Public Member Functions

DLLEXPORT Random ()
 
DLLEXPORT Random (int seed)
 
DLLEXPORT ~Random ()
 
DLLEXPORT int GetSeed ()
 
DLLEXPORT int GetNumber ()
 
DLLEXPORT int GetNumber (int min, int max)
 Get a random in range [min, max]. More...
 
DLLEXPORT float GetNumber (float min, float max)
 
DLLEXPORT float GetFloat (float min, float max)
 
DLLEXPORT float SymmetricRandom ()
 
DLLEXPORT void SetSeed (int seed)
 
DLLEXPORT void SetIndex (int index)
 
DLLEXPORT int GetIndex ()
 
DLLEXPORT void SetAsMain ()
 

Static Public Member Functions

static DLLEXPORT RandomGet ()
 

Detailed Description

Random number generator based on Mersenne Twister.

Definition at line 15 of file Random.h.

Constructor & Destructor Documentation

◆ Random() [1/2]

Leviathan::Random::Random ( )

Definition at line 25 of file Random.cpp.

26 {
27  Index = 0;
28 
29  // no seed provided so we must use current system time as seed //
30 #ifdef _WIN32
31  // structures to get time //
32  SYSTEMTIME time;
33  GetSystemTime(&time);
34  // just mash something together from current time //
35  Seed =
36  2500 + time.wDay * 25 + time.wHour * 15 + time.wMilliseconds * 50 + time.wSecond * 2;
37 #else
38  timespec time;
39 
40  clock_gettime(CLOCK_REALTIME, &time);
41 
42  Seed = time.tv_nsec + time.tv_sec * 2;
43 
44 #endif
45 
46  // initialize numbers //
47  _InitializeGenerator();
48 }

◆ Random() [2/2]

Leviathan::Random::Random ( int  seed)

Definition at line 50 of file Random.cpp.

51 {
52  // just save values and initialize it //
53  Index = 0;
54  Seed = seed;
55 
56  _InitializeGenerator();
57 }

◆ ~Random()

Leviathan::Random::~Random ( )

Definition at line 59 of file Random.cpp.

60 {
61  // we don't really need to delete anything //
62 
63  // check is static access this and set to null if it is //
64  if(staticaccess == this)
65  staticaccess = NULL;
66 }

Member Function Documentation

◆ Get()

Random * Leviathan::Random::Get ( )
static

Definition at line 68 of file Random.cpp.

69 {
70  return staticaccess;
71 }

◆ GetFloat()

DLLEXPORT float Leviathan::Random::GetFloat ( float  min,
float  max 
)
inline

Basically the same as GetNumber but avoids requiring explicit casts to get the float variant

Definition at line 31 of file Random.h.

32  {
33 
34  return GetNumber(min, max);
35  }
DLLEXPORT int GetNumber()
Definition: Random.cpp:80

◆ GetIndex()

int Leviathan::Random::GetIndex ( )

Definition at line 139 of file Random.cpp.

140 {
141  return Index;
142 }

◆ GetNumber() [1/3]

int Leviathan::Random::GetNumber ( )

Definition at line 80 of file Random.cpp.

81 {
82  // gets the number from index //
83 
84  if(Index == 0) {
85  // needs new set of numbers //
86  _GenerateNumbers();
87  }
88 
89  int y = MT[Index];
90 
91  // randomize the result more here //
92 
93  y = y ^ (y >> 11);
94  y = y ^ ((y << 7) & (0x9d2c5680));
95  y = y ^ ((y << 15) & (0xefc60000));
96  y = y ^ (y >> 18);
97 
98  // increase index //
99  Index = (Index + 1) % RANDOM_STORAGE_SIZE;
100  return y;
101 }
#define RANDOM_STORAGE_SIZE
Definition: Random.h:10

◆ GetNumber() [2/3]

int Leviathan::Random::GetNumber ( int  min,
int  max 
)

Get a random in range [min, max].

Definition at line 103 of file Random.cpp.

104 {
105  // New, hopefully better approach for smaller ranges
106  // See stuff here:
107  // https://stackoverflow.com/questions/1202687/how-do-i-get-a-specific-range-of-numbers-from-rand
108  return static_cast<int>(min + (GetNumber() / (RANDOM_MAX_POSSIBLE / (max - min + 1) + 1.0f)));
109 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:80
#define RANDOM_MAX_POSSIBLE
Definition: Random.h:9

◆ GetNumber() [3/3]

float Leviathan::Random::GetNumber ( float  min,
float  max 
)

Definition at line 111 of file Random.cpp.

112 {
113  // same as above but with floats //
114 
115  // get number and sample it to be between given values //
116  // basically get percentage of max value and then get difference of min and max and
117  // multiply that by percents and add it to min to get value between min and max
118  return min + (((float)GetNumber() / RANDOM_MAX_POSSIBLE) * (max - min));
119 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:80
#define RANDOM_MAX_POSSIBLE
Definition: Random.h:9

◆ GetSeed()

int Leviathan::Random::GetSeed ( )

Definition at line 75 of file Random.cpp.

76 {
77  return Seed;
78 }

◆ SetAsMain()

void Leviathan::Random::SetAsMain ( )

Definition at line 144 of file Random.cpp.

145 {
146  if(staticaccess != NULL)
147  Logger::Get()->Info("Random: old static access replaced by new");
148  staticaccess = this;
149 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ SetIndex()

void Leviathan::Random::SetIndex ( int  index)

Definition at line 134 of file Random.cpp.

135 {
136  Index = index % RANDOM_STORAGE_SIZE;
137 }
#define RANDOM_STORAGE_SIZE
Definition: Random.h:10

◆ SetSeed()

void Leviathan::Random::SetSeed ( int  seed)

Definition at line 126 of file Random.cpp.

127 {
128  // this acts the same as generating new object with the provided seed //
129  Seed = seed;
130  Index = 0;
131  _InitializeGenerator();
132 }

◆ SymmetricRandom()

DLLEXPORT float Leviathan::Random::SymmetricRandom ( )

Definition at line 121 of file Random.cpp.

122 {
123  return 2.f * GetNumber(0.f, 1.f) - 1.f;
124 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:80

The documentation for this class was generated from the following files: