TrinityCore
Loading...
Searching...
No Matches
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 TRINITYCORE_RANDOM_H
19#define TRINITYCORE_RANDOM_H
20
21#include "Define.h"
22#include "Duration.h"
23#include <concepts>
24#include <limits>
25
26/* Return a random number in the range min..max. */
28
29/* Return a random number in the range min..max (inclusive). */
31
32/* Return a random millisecond value between min and max seconds. Functionally equivalent to urand(min*IN_MILLISECONDS, max*IN_MILLISECONDS). */
34
35/* Return a random number in the range 0 .. UINT32_MAX. */
37
38/* Return a random time in the range min..max (up to millisecond precision). Only works for values where millisecond difference is a valid uint32. */
40
41/* Return a random number in the range min..max */
42TC_COMMON_API float frand(float min, float max);
43
44/* Return a random float from 0.0 to 1.0 (exclusive). */
46
47/* Return a random float from 0.0 to 100.0 (exclusive). */
49
50/* Return a random number in the range 0..count (exclusive) with each value having a different chance of happening */
51TC_COMMON_API uint32 urandweighted(size_t count, double const* chances);
52
53/* Return true if a random roll fits in the specified chance (range 0-100). */
54template <std::floating_point T>
55inline bool roll_chance(T chance)
56{
57 return chance > rand_chance();
58}
59
60/* Return true if a random roll fits in the specified chance (range 0-100). */
61template <std::signed_integral T>
62inline bool roll_chance(T chance)
63{
64 return chance > irand(0, 99);
65}
66
67/* Return true if a random roll fits in the specified chance (range 0-100). */
68template <std::unsigned_integral T>
69inline bool roll_chance(T chance)
70{
71 return chance > urand(0, 99);
72}
73
74/*
75* Wrapper satisfying UniformRandomNumberGenerator concept for use in <random> algorithms
76*/
78{
79public:
81
82 static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
83 static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
84 result_type operator()() const { return rand32(); }
85};
86
88{
89 float AccumulateChance(float progress) { return _progress += progress; }
90 void Reset() { _progress = 0; }
91
92private:
93 float _progress = 0.0f;
94};
95
96/* Return true if a random roll fits in the specified chance (range 0-100) using pseudo random distribution that minimizes long good/bad luck streaks. */
98
99#endif // TRINITYCORE_RANDOM_H
#define TC_COMMON_API
Definition Define.h:99
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
TC_COMMON_API uint32 urandms(uint32 min, uint32 max)
Definition Random.cpp:50
TC_COMMON_API float frand(float min, float max)
Definition Random.cpp:56
TC_COMMON_API uint32 rand32()
Definition Random.cpp:71
TC_COMMON_API float rand_norm()
Definition Random.cpp:76
TC_COMMON_API Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition Random.cpp:63
TC_COMMON_API int32 irand(int32 min, int32 max)
Definition Random.cpp:36
TC_COMMON_API uint32 urandweighted(size_t count, double const *chances)
Definition Random.cpp:88
TC_COMMON_API uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:43
bool roll_chance(T chance)
Definition Random.h:55
TC_COMMON_API float rand_chance()
Definition Random.cpp:82
uint32 result_type
Definition Random.h:80
static constexpr result_type min()
Definition Random.h:82
static constexpr result_type max()
Definition Random.h:83
result_type operator()() const
Definition Random.h:84
float AccumulateChance(float progress)
Definition Random.h:89