TrinityCore
Random.cpp File Reference
#include "Random.h"
#include "Errors.h"
#include "SFMTRand.h"
#include <memory>
#include <random>
+ Include dependency graph for Random.cpp:

Functions

static SFMTRandGetRng ()
 
int32 irand (int32 min, int32 max)
 
uint32 urand (uint32 min, uint32 max)
 
uint32 urandms (uint32 min, uint32 max)
 
float frand (float min, float max)
 
Milliseconds randtime (Milliseconds min, Milliseconds max)
 
uint32 rand32 ()
 
double rand_norm ()
 
double rand_chance ()
 
uint32 urandweighted (size_t count, double const *chances)
 

Variables

static thread_local std::unique_ptr< SFMTRandsfmtRand
 
static RandomEngine engine
 

Function Documentation

◆ frand()

float frand ( float  min,
float  max 
)
56 {
57  ASSERT(max >= min);
58  std::uniform_real_distribution<float> urd(min, max);
59  return urd(engine);
60 }
static RandomEngine engine
Definition: Random.cpp:25
#define ASSERT
Definition: Errors.h:68
+ Here is the caller graph for this function:

◆ GetRng()

static SFMTRand* GetRng ( )
static
28 {
29  if (!sfmtRand)
30  sfmtRand = std::make_unique<SFMTRand>();
31 
32  return sfmtRand.get();
33 }
static thread_local std::unique_ptr< SFMTRand > sfmtRand
Definition: Random.cpp:24
+ Here is the caller graph for this function:

◆ irand()

int32 irand ( int32  min,
int32  max 
)
36 {
37  ASSERT(max >= min);
38  std::uniform_int_distribution<int32> uid(min, max);
39  return uid(engine);
40 }
static RandomEngine engine
Definition: Random.cpp:25
#define ASSERT
Definition: Errors.h:68
+ Here is the caller graph for this function:

◆ rand32()

uint32 rand32 ( )
71 {
72  return GetRng()->RandomUInt32();
73 }
uint32 RandomUInt32()
Definition: SFMTRand.cpp:74
static SFMTRand * GetRng()
Definition: Random.cpp:27
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rand_chance()

double rand_chance ( )
82 {
83  std::uniform_real_distribution<double> urd(0.0, 100.0);
84  return urd(engine);
85 }
static RandomEngine engine
Definition: Random.cpp:25
+ Here is the caller graph for this function:

◆ rand_norm()

double rand_norm ( )
76 {
77  std::uniform_real_distribution<double> urd;
78  return urd(engine);
79 }
static RandomEngine engine
Definition: Random.cpp:25
+ Here is the caller graph for this function:

◆ randtime()

Milliseconds randtime ( Milliseconds  min,
Milliseconds  max 
)
63 {
64  long long diff = max.count() - min.count();
65  ASSERT(diff >= 0);
66  ASSERT(diff <= 0xFFFFFFFF);
67  return min + Milliseconds(urand(0, uint32(diff)));
68 }
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:24
uint32_t uint32
Definition: Define.h:143
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
#define ASSERT
Definition: Errors.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urand()

uint32 urand ( uint32  min,
uint32  max 
)
43 {
44  ASSERT(max >= min);
45  std::uniform_int_distribution<uint32> uid(min, max);
46  return uid(engine);
47 }
static RandomEngine engine
Definition: Random.cpp:25
#define ASSERT
Definition: Errors.h:68

◆ urandms()

uint32 urandms ( uint32  min,
uint32  max 
)
50 {
51  ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
52  return urand(min * Milliseconds::period::den, max * Milliseconds::period::den);
53 }
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
#define ASSERT
Definition: Errors.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urandweighted()

uint32 urandweighted ( size_t  count,
double const *  chances 
)
88 {
89  std::discrete_distribution<uint32> dd(chances, chances + count);
90  return dd(engine);
91 }
static RandomEngine engine
Definition: Random.cpp:25
+ Here is the caller graph for this function:

Variable Documentation

◆ engine

RandomEngine engine
static

◆ sfmtRand

thread_local std::unique_ptr<SFMTRand> sfmtRand
static