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:
Ovahlord
2022-03-10 23:10:49 +01:00
parent a6809465b5
commit 7a2e26d42e
2 changed files with 11 additions and 2 deletions

View File

@@ -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;

View File

@@ -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));
}
/*