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)
 
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.

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

◆ Random() [2/2]

Leviathan::Random::Random ( int  seed)

Definition at line 48 of file Random.cpp.

48  {
49  // just save values and initialize it //
50  Index = 0;
51  Seed = seed;
52 
53  _InitializeGenerator();
54 }

◆ ~Random()

Leviathan::Random::~Random ( )

Definition at line 56 of file Random.cpp.

56  {
57  // we don't really need to delete anything //
58 
59  // check is static access this and set to null if it is //
60  if(staticaccess == this)
61  staticaccess = NULL;
62 }

Member Function Documentation

◆ Get()

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

Definition at line 64 of file Random.cpp.

64  {
65  return staticaccess;
66 }

◆ 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 30 of file Random.h.

30  {
31 
32  return GetNumber(min, max);
33  }
DLLEXPORT int GetNumber()
Definition: Random.cpp:74

◆ GetIndex()

int Leviathan::Random::GetIndex ( )

Definition at line 127 of file Random.cpp.

127  {
128  return Index;
129 }

◆ GetNumber() [1/3]

int Leviathan::Random::GetNumber ( )

Definition at line 74 of file Random.cpp.

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

◆ GetNumber() [2/3]

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

Definition at line 96 of file Random.cpp.

96  {
97  // get number and sample it to be between given values //
98  // basically get percentage of max value and then get difference of min and max and multiply
99  // that by percents and add it to min to get value between min and max
100  return (int)(min+(((float)GetNumber()/RANDOM_MAX_POSSIBLE)*(max-min)));
101 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:74
#define RANDOM_MAX_POSSIBLE
Definition: Random.h:9

◆ GetNumber() [3/3]

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

Definition at line 103 of file Random.cpp.

103  {
104  // same as above but with floats //
105 
106  // get number and sample it to be between given values //
107  // basically get percentage of max value and then get difference of min and max and multiply
108  // that by percents and add it to min to get value between min and max
109  return min+(((float)GetNumber()/RANDOM_MAX_POSSIBLE)*(max-min));
110 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:74
#define RANDOM_MAX_POSSIBLE
Definition: Random.h:9

◆ GetSeed()

int Leviathan::Random::GetSeed ( )

Definition at line 70 of file Random.cpp.

70  {
71  return Seed;
72 }

◆ SetAsMain()

void Leviathan::Random::SetAsMain ( )

Definition at line 131 of file Random.cpp.

131  {
132  if(staticaccess != NULL)
133  Logger::Get()->Info("Random: old static access replaced by new");
134  staticaccess = this;
135 }
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 123 of file Random.cpp.

123  {
124  Index = index % RANDOM_STORAGE_SIZE;
125 }
#define RANDOM_STORAGE_SIZE
Definition: Random.h:10

◆ SetSeed()

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

Definition at line 116 of file Random.cpp.

116  {
117  // this acts the same as generating new object with the provided seed //
118  Seed = seed;
119  Index = 0;
120  _InitializeGenerator();
121 }

◆ SymmetricRandom()

DLLEXPORT float Leviathan::Random::SymmetricRandom ( )

Definition at line 112 of file Random.cpp.

112  {
113  return 2.f*GetNumber(0.f, 1.f)-1.f;
114 }
DLLEXPORT int GetNumber()
Definition: Random.cpp:74

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