2015-12-24 19:48:39 +01:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2015-12-24 19:48:39 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Random.h"
|
|
|
|
|
#include "Errors.h"
|
2019-10-30 10:42:08 +01:00
|
|
|
#include "SFMTRand.h"
|
2020-07-18 19:34:45 +00:00
|
|
|
#include <memory>
|
2017-03-21 21:04:01 +01:00
|
|
|
#include <random>
|
2015-12-24 19:48:39 +01:00
|
|
|
|
2020-07-18 19:34:45 +00:00
|
|
|
static thread_local std::unique_ptr<SFMTRand> sfmtRand;
|
2019-11-09 13:32:58 +01:00
|
|
|
static RandomEngine engine;
|
2015-12-24 19:48:39 +01:00
|
|
|
|
|
|
|
|
static SFMTRand* GetRng()
|
|
|
|
|
{
|
2020-07-18 19:34:45 +00:00
|
|
|
if (!sfmtRand)
|
|
|
|
|
sfmtRand = std::make_unique<SFMTRand>();
|
2015-12-24 19:48:39 +01:00
|
|
|
|
2020-07-18 19:34:45 +00:00
|
|
|
return sfmtRand.get();
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32 irand(int32 min, int32 max)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(max >= min);
|
2019-11-09 13:32:58 +01:00
|
|
|
std::uniform_int_distribution<int32> uid(min, max);
|
|
|
|
|
return uid(engine);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32 urand(uint32 min, uint32 max)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(max >= min);
|
2019-11-09 13:32:58 +01:00
|
|
|
std::uniform_int_distribution<uint32> uid(min, max);
|
|
|
|
|
return uid(engine);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32 urandms(uint32 min, uint32 max)
|
|
|
|
|
{
|
2017-05-11 22:29:51 +02:00
|
|
|
ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max);
|
2019-11-09 13:32:58 +01:00
|
|
|
return urand(min * Milliseconds::period::den, max * Milliseconds::period::den);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float frand(float min, float max)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(max >= min);
|
2019-11-09 13:32:58 +01:00
|
|
|
std::uniform_real_distribution<float> urd(min, max);
|
|
|
|
|
return urd(engine);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:48:15 +02:00
|
|
|
Milliseconds randtime(Milliseconds min, Milliseconds max)
|
2016-03-01 23:09:25 +01:00
|
|
|
{
|
|
|
|
|
long long diff = max.count() - min.count();
|
|
|
|
|
ASSERT(diff >= 0);
|
2017-06-07 17:23:50 +02:00
|
|
|
ASSERT(diff <= 0xFFFFFFFF);
|
|
|
|
|
return min + Milliseconds(urand(0, uint32(diff)));
|
2016-03-01 23:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-24 19:48:39 +01:00
|
|
|
uint32 rand32()
|
|
|
|
|
{
|
2019-11-09 13:32:58 +01:00
|
|
|
return GetRng()->RandomUInt32();
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 20:20:00 +01:00
|
|
|
float rand_norm()
|
2015-12-24 19:48:39 +01:00
|
|
|
{
|
2023-10-31 20:20:00 +01:00
|
|
|
std::uniform_real_distribution<float> urd;
|
2019-11-09 13:32:58 +01:00
|
|
|
return urd(engine);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 20:20:00 +01:00
|
|
|
float rand_chance()
|
2015-12-24 19:48:39 +01:00
|
|
|
{
|
2023-10-31 20:20:00 +01:00
|
|
|
std::uniform_real_distribution<float> urd(0.0f, 100.0f);
|
2019-11-09 13:32:58 +01:00
|
|
|
return urd(engine);
|
2015-12-24 19:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
2017-03-21 21:04:01 +01:00
|
|
|
uint32 urandweighted(size_t count, double const* chances)
|
|
|
|
|
{
|
|
|
|
|
std::discrete_distribution<uint32> dd(chances, chances + count);
|
2019-11-09 13:32:58 +01:00
|
|
|
return dd(engine);
|
2017-03-21 21:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-09 13:32:58 +01:00
|
|
|
RandomEngine& RandomEngine::Instance()
|
2015-12-24 19:48:39 +01:00
|
|
|
{
|
|
|
|
|
return engine;
|
|
|
|
|
}
|