Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
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

24
static thread_local std::unique_ptr<SFMTRand> sfmtRand;
25
static RandomEngine engine;
26

27
static SFMTRand* GetRng()
28
{
29
    if (!sfmtRand)
30
        sfmtRand = std::make_unique<SFMTRand>();
31

32
    return sfmtRand.get();
33
}
34

35
int32 irand(int32 min, int32 max)
36
{
37
    ASSERT(max >= min);
38
    std::uniform_int_distribution<int32> uid(min, max);
39
    return uid(engine);
40
}
41

42
uint32 urand(uint32 min, uint32 max)
43
{
44
    ASSERT(max >= min);
45
    std::uniform_int_distribution<uint32> uid(min, max);
46
    return uid(engine);
47
}
48

49
uint32 urandms(uint32 min, uint32 max)
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

55
float 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

62
Milliseconds randtime(Milliseconds min, Milliseconds max)
63
{
64
    long long diff = max.count() - min.count();
65
    ASSERT(diff >= 0);
66
    ASSERT(diff <= (uint32)-1);
67
    return min + Milliseconds(urand(0, diff));
68
}
69

70
uint32 rand32()
71
{
72
    return GetRng()->RandomUInt32();
73
}
74

75
double rand_norm()
76
{
77
    std::uniform_real_distribution<double> urd;
78
    return urd(engine);
79
}
80

81
double rand_chance()
82
{
83
    std::uniform_real_distribution<double> urd(0.0, 100.0);
84
    return urd(engine);
85
}
86

87
uint32 urandweighted(size_t count, double const* chances)
88
{
89
    std::discrete_distribution<uint32> dd(chances, chances + count);
90
    return dd(engine);
91
}
92

93
RandomEngine& RandomEngine::Instance()
94
{
95
    return engine;
96
}
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.