TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Random.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef Random_h__
19 #define Random_h__
20 
21 #include "Define.h"
22 #include <limits>
23 #include <random>
24 
25 /* Return a random number in the range min..max. */
27 
28 /* Return a random number in the range min..max (inclusive). */
30 
31 /* Return a random millisecond value between min and max seconds. Functionally equivalent to urand(min*IN_MILLISECONDS, max*IN_MILLISECONDS). */
33 
34 /* Return a random number in the range 0 .. UINT32_MAX. */
36 
37 /* Return a random number in the range min..max */
38 TC_COMMON_API float frand(float min, float max);
39 
40 /* Return a random double from 0.0 to 1.0 (exclusive). */
41 TC_COMMON_API double rand_norm();
42 
43 /* Return a random double from 0.0 to 100.0 (exclusive). */
44 TC_COMMON_API double rand_chance();
45 
46 /* Return true if a random roll fits in the specified chance (range 0-100). */
47 inline bool roll_chance_f(float chance)
48 {
49  return chance > rand_chance();
50 }
51 
52 /* Return true if a random roll fits in the specified chance (range 0-100). */
53 inline bool roll_chance_i(int chance)
54 {
55  return chance > irand(0, 99);
56 }
57 
58 /*
59 * SFMT wrapper satisfying UniformRandomNumberGenerator concept for use in <random> algorithms
60 */
62 {
63 public:
65 
66  static TRINITY_CONSTEXPR result_type min() { return std::numeric_limits<result_type>::min(); }
67  static TRINITY_CONSTEXPR result_type max() { return std::numeric_limits<result_type>::max(); }
68  result_type operator()() const { return rand32(); }
69 
70  static SFMTEngine& Instance();
71 };
72 
73 // Ugly, horrible, i don't even..., hack for VS2013 to work around missing discrete_distribution(iterator, iterator) constructor
74 namespace Trinity
75 {
76 #if COMPILER == COMPILER_MICROSOFT && _MSC_VER <= 1800
77  template<typename T>
78  struct discrete_distribution_param : public std::discrete_distribution<T>::param_type
79  {
80  typedef typename std::discrete_distribution<T>::param_type base;
81 
82  template<typename InIt>
83  discrete_distribution_param(InIt begin, InIt end) : base(_Noinit())
84  {
85  this->_Pvec.assign(begin, end);
86  this->_Init();
87  }
88  };
89 #else
90  template<typename T>
91  using discrete_distribution_param = typename std::discrete_distribution<T>::param_type;
92 #endif
93 }
94 
95 #endif // Random_h__
TC_COMMON_API float frand(float min, float max)
Definition: Random.cpp:58
static TRINITY_CONSTEXPR result_type min()
Definition: Random.h:66
Definition: Random.h:61
discrete_distribution_param(InIt begin, InIt end)
Definition: Random.h:83
TC_COMMON_API int32 irand(int32 min, int32 max)
Definition: Random.cpp:39
static TRINITY_CONSTEXPR result_type max()
Definition: Random.h:67
T max(const T &x, const T &y)
Definition: g3dmath.h:320
TC_COMMON_API uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:45
T min(const T &x, const T &y)
Definition: g3dmath.h:305
TC_COMMON_API uint32 rand32()
Definition: Random.cpp:64
bool roll_chance_f(float chance)
Definition: Random.h:47
TC_COMMON_API double rand_chance()
Definition: Random.cpp:74
#define TRINITY_CONSTEXPR
Definition: Define.h:94
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
TC_COMMON_API double rand_norm()
Definition: Random.cpp:69
bool roll_chance_i(int chance)
Definition: Random.h:53
result_type operator()() const
Definition: Random.h:68
#define TC_COMMON_API
Definition: Define.h:116
TC_COMMON_API uint32 urandms(uint32 min, uint32 max)
Definition: Random.cpp:51
std::discrete_distribution< T >::param_type base
Definition: Random.h:80
Definition: Common.h:172
uint32 result_type
Definition: Random.h:64