TrinityCore
Loading...
Searching...
No Matches
Random.h File Reference
#include "Define.h"
#include "Duration.h"
#include <limits>
+ Include dependency graph for Random.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  RandomEngine
 

Functions

TC_COMMON_API int32 irand (int32 min, int32 max)
 
TC_COMMON_API uint32 urand (uint32 min, uint32 max)
 
TC_COMMON_API uint32 urandms (uint32 min, uint32 max)
 
TC_COMMON_API uint32 rand32 ()
 
TC_COMMON_API Milliseconds randtime (Milliseconds min, Milliseconds max)
 
TC_COMMON_API float frand (float min, float max)
 
TC_COMMON_API double rand_norm ()
 
TC_COMMON_API double rand_chance ()
 
TC_COMMON_API uint32 urandweighted (size_t count, double const *chances)
 
bool roll_chance_f (float chance)
 
bool roll_chance_i (int chance)
 

Function Documentation

◆ frand()

TC_COMMON_API 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:

◆ irand()

TC_COMMON_API 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()

TC_COMMON_API 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()

TC_COMMON_API 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()

TC_COMMON_API 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()

TC_COMMON_API 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:

◆ roll_chance_f()

bool roll_chance_f ( float  chance)
inline

Definition at line 53 of file Random.h.

54{
55 return chance > rand_chance();
56}
TC_COMMON_API double rand_chance()
Definition: Random.cpp:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ roll_chance_i()

bool roll_chance_i ( int  chance)
inline

Definition at line 59 of file Random.h.

60{
61 return chance > irand(0, 99);
62}
TC_COMMON_API int32 irand(int32 min, int32 max)
Definition: Random.cpp:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ urand()

TC_COMMON_API 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()

TC_COMMON_API 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()

TC_COMMON_API 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: