TrinityCore
Random.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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 "Duration.h"
23#include <limits>
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 time in the range min..max (up to millisecond precision). Only works for values where millisecond difference is a valid uint32. */
39
40/* Return a random number in the range min..max */
41TC_COMMON_API float frand(float min, float max);
42
43/* Return a random float from 0.0 to 1.0 (exclusive). */
45
46/* Return a random float from 0.0 to 100.0 (exclusive). */
48
49/* Return a random number in the range 0..count (exclusive) with each value having a different chance of happening */
50TC_COMMON_API uint32 urandweighted(size_t count, double const* chances);
51
52/* Return true if a random roll fits in the specified chance (range 0-100). */
53inline bool roll_chance_f(float chance)
54{
55 return chance > rand_chance();
56}
57
58/* Return true if a random roll fits in the specified chance (range 0-100). */
59inline bool roll_chance_i(int chance)
60{
61 return chance > irand(0, 99);
62}
63
64/*
65* Wrapper satisfying UniformRandomNumberGenerator concept for use in <random> algorithms
66*/
68{
69public:
71
72 static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
73 static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
74 result_type operator()() const { return rand32(); }
75
76 static RandomEngine& Instance();
77};
78
79#endif // Random_h__
#define TC_COMMON_API
Definition: Define.h:99
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
TC_COMMON_API uint32 urandms(uint32 min, uint32 max)
Definition: Random.cpp:49
TC_COMMON_API float frand(float min, float max)
Definition: Random.cpp:55
TC_COMMON_API uint32 rand32()
Definition: Random.cpp:70
bool roll_chance_f(float chance)
Definition: Random.h:53
TC_COMMON_API float rand_norm()
Definition: Random.cpp:75
bool roll_chance_i(int chance)
Definition: Random.h:59
TC_COMMON_API Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition: Random.cpp:62
TC_COMMON_API int32 irand(int32 min, int32 max)
Definition: Random.cpp:35
TC_COMMON_API uint32 urandweighted(size_t count, double const *chances)
Definition: Random.cpp:87
TC_COMMON_API uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
TC_COMMON_API float rand_chance()
Definition: Random.cpp:81
uint32 result_type
Definition: Random.h:70
static constexpr result_type min()
Definition: Random.h:72
static constexpr result_type max()
Definition: Random.h:73
result_type operator()() const
Definition: Random.h:74