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

Go to the source code of this file.

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 
)

Definition at line 55 of file Random.cpp.

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

◆ GetRng()

static SFMTRand * GetRng ( )
static

Definition at line 27 of file Random.cpp.

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 
)

Definition at line 35 of file Random.cpp.

36{
37 ASSERT(max >= min);
38 std::uniform_int_distribution<int32> uid(min, max);
39 return uid(engine);
40}
+ Here is the caller graph for this function:

◆ rand32()

uint32 rand32 ( )

Definition at line 70 of file Random.cpp.

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

◆ rand_chance()

double rand_chance ( )

Definition at line 81 of file Random.cpp.

82{
83 std::uniform_real_distribution<double> urd(0.0, 100.0);
84 return urd(engine);
85}
+ Here is the caller graph for this function:

◆ rand_norm()

double rand_norm ( )

Definition at line 75 of file Random.cpp.

76{
77 std::uniform_real_distribution<double> urd;
78 return urd(engine);
79}
+ Here is the caller graph for this function:

◆ randtime()

Milliseconds randtime ( Milliseconds  min,
Milliseconds  max 
)

Definition at line 62 of file Random.cpp.

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}
uint32_t uint32
Definition: Define.h:143
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urand()

uint32 urand ( uint32  min,
uint32  max 
)

Definition at line 42 of file Random.cpp.

43{
44 ASSERT(max >= min);
45 std::uniform_int_distribution<uint32> uid(min, max);
46 return uid(engine);
47}

◆ urandms()

uint32 urandms ( uint32  min,
uint32  max 
)

Definition at line 49 of file Random.cpp.

50{
51 ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
52 return urand(min * Milliseconds::period::den, max * Milliseconds::period::den);
53}
+ 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 
)

Definition at line 87 of file Random.cpp.

88{
89 std::discrete_distribution<uint32> dd(chances, chances + count);
90 return dd(engine);
91}
+ Here is the caller graph for this function:

Variable Documentation

◆ engine

RandomEngine engine
static

Definition at line 25 of file Random.cpp.

◆ sfmtRand

thread_local std::unique_ptr<SFMTRand> sfmtRand
static

Definition at line 24 of file Random.cpp.