Legends-of-Azeroth-Pandaria-5.4.8
96 строк · 2.3 Кб
1/*
2* This file is part of the Pandaria 5.4.8 Project. See THANKS 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;
25static RandomEngine engine;
26
27static SFMTRand* GetRng()
28{
29if (!sfmtRand)
30sfmtRand = std::make_unique<SFMTRand>();
31
32return sfmtRand.get();
33}
34
35int32 irand(int32 min, int32 max)
36{
37ASSERT(max >= min);
38std::uniform_int_distribution<int32> uid(min, max);
39return uid(engine);
40}
41
42uint32 urand(uint32 min, uint32 max)
43{
44ASSERT(max >= min);
45std::uniform_int_distribution<uint32> uid(min, max);
46return uid(engine);
47}
48
49uint32 urandms(uint32 min, uint32 max)
50{
51ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
52return urand(min * Milliseconds::period::den, max * Milliseconds::period::den);
53}
54
55float frand(float min, float max)
56{
57ASSERT(max >= min);
58std::uniform_real_distribution<float> urd(min, max);
59return urd(engine);
60}
61
62Milliseconds randtime(Milliseconds min, Milliseconds max)
63{
64long long diff = max.count() - min.count();
65ASSERT(diff >= 0);
66ASSERT(diff <= (uint32)-1);
67return min + Milliseconds(urand(0, diff));
68}
69
70uint32 rand32()
71{
72return GetRng()->RandomUInt32();
73}
74
75double rand_norm()
76{
77std::uniform_real_distribution<double> urd;
78return urd(engine);
79}
80
81double rand_chance()
82{
83std::uniform_real_distribution<double> urd(0.0, 100.0);
84return urd(engine);
85}
86
87uint32 urandweighted(size_t count, double const* chances)
88{
89std::discrete_distribution<uint32> dd(chances, chances + count);
90return dd(engine);
91}
92
93RandomEngine& RandomEngine::Instance()
94{
95return engine;
96}
97