mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-02-05 08:28:57 +01:00
Core/Random: use std::bernoulli_distribution for roll_chance_ calls to get more reliable roll results for yes-no scenarios
This commit is contained in:
@@ -90,6 +90,12 @@ uint32 urandweighted(size_t count, double const* chances)
|
||||
return dd(engine);
|
||||
}
|
||||
|
||||
bool coin_flip(float chance)
|
||||
{
|
||||
std::bernoulli_distribution bd { chance / 100.0 };
|
||||
return bd(engine);
|
||||
}
|
||||
|
||||
RandomEngine& RandomEngine::Instance()
|
||||
{
|
||||
return engine;
|
||||
|
||||
@@ -50,16 +50,19 @@ TC_COMMON_API double rand_chance();
|
||||
/* Return a random number in the range 0..count (exclusive) with each value having a different chance of happening */
|
||||
TC_COMMON_API uint32 urandweighted(size_t count, double const* chances);
|
||||
|
||||
/* Return true if a coin flip with the given chance of receiving tails occours*/
|
||||
TC_COMMON_API bool coin_flip(float chance);
|
||||
|
||||
/* Return true if a random roll fits in the specified chance (range 0-100). */
|
||||
inline bool roll_chance_f(float chance)
|
||||
{
|
||||
return chance > rand_chance();
|
||||
return coin_flip(chance);
|
||||
}
|
||||
|
||||
/* Return true if a random roll fits in the specified chance (range 0-100). */
|
||||
inline bool roll_chance_i(int chance)
|
||||
{
|
||||
return chance > irand(0, 99);
|
||||
return coin_flip(static_cast<float>(chance));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user