TrinityCore
Random.cpp
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#include "Random.h"
19#include "Errors.h"
20#include "SFMTRand.h"
21#include <memory>
22#include <random>
23
24static thread_local std::unique_ptr<SFMTRand> sfmtRand;
26
28{
29 if (!sfmtRand)
30 sfmtRand = std::make_unique<SFMTRand>();
31
32 return sfmtRand.get();
33}
34
36{
37 ASSERT(max >= min);
38 std::uniform_int_distribution<int32> uid(min, max);
39 return uid(engine);
40}
41
43{
44 ASSERT(max >= min);
45 std::uniform_int_distribution<uint32> uid(min, max);
46 return uid(engine);
47}
48
50{
51 ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
52 return urand(min * Milliseconds::period::den, max * Milliseconds::period::den);
53}
54
55float frand(float min, float max)
56{
57 ASSERT(max >= min);
58 std::uniform_real_distribution<float> urd(min, max);
59 return urd(engine);
60}
61
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}
69
71{
72 return GetRng()->RandomUInt32();
73}
74
75float rand_norm()
76{
77 std::uniform_real_distribution<float> urd;
78 return urd(engine);
79}
80
82{
83 std::uniform_real_distribution<float> urd(0.0f, 100.0f);
84 return urd(engine);
85}
86
87uint32 urandweighted(size_t count, double const* chances)
88{
89 std::discrete_distribution<uint32> dd(chances, chances + count);
90 return dd(engine);
91}
92
94{
95 return engine;
96}
int32_t int32
Definition: Define.h:139
uint32_t uint32
Definition: Define.h:143
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
#define ASSERT
Definition: Errors.h:68
static thread_local std::unique_ptr< SFMTRand > sfmtRand
Definition: Random.cpp:24
float frand(float min, float max)
Definition: Random.cpp:55
uint32 urandweighted(size_t count, double const *chances)
Definition: Random.cpp:87
int32 irand(int32 min, int32 max)
Definition: Random.cpp:35
float rand_chance()
Definition: Random.cpp:81
static SFMTRand * GetRng()
Definition: Random.cpp:27
static RandomEngine engine
Definition: Random.cpp:25
Milliseconds randtime(Milliseconds min, Milliseconds max)
Definition: Random.cpp:62
float rand_norm()
Definition: Random.cpp:75
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:42
uint32 rand32()
Definition: Random.cpp:70
uint32 urandms(uint32 min, uint32 max)
Definition: Random.cpp:49
static RandomEngine & Instance()
Definition: Random.cpp:93
uint32 RandomUInt32()
Definition: SFMTRand.cpp:70