Skip to content

Commit 0bf8a73

Browse files
committed
Move randomChance implementation to helpers namespace
1 parent dcf0b09 commit 0bf8a73

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

libs/common/include/helpers/random.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ T randomValue(RandomT& rng, T min = std::numeric_limits<T>::min(), T max = std::
2424
return static_cast<T>(distr(rng));
2525
}
2626

27+
/// Return a true in `chance` out of `total` cases:
28+
/// randomChance() ... return true|false with 50% chance each
29+
/// randomChance(15) ... return true in 1/15 of the cases
30+
/// randomChance(20, 5) ... return true in 5 out of 20 cases, i.e. a probability of 25%. Sames as randomChance(4, 1)
31+
template<typename RandomT>
32+
bool randomChance(RandomT& rng, unsigned total = 2u, unsigned chance = 1u)
33+
{
34+
RTTR_Assert(total > 0u);
35+
return (chance >= total) || randomValue(rng, 1u, total) <= chance;
36+
}
37+
2738
/// Return a random enumerator from the given enum
2839
template<typename T, typename RandomT>
2940
T randomEnum(RandomT& rng)

libs/s25main/ai/random.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ T randomValue(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits
2424
/// randomChance(20, 5) ... return true in 5 out of 20 cases, i.e. a probability of 25%. Sames as randomChance(4, 1)
2525
inline bool randomChance(unsigned total = 2u, unsigned chance = 1u)
2626
{
27-
RTTR_Assert(total > 0u);
28-
return (chance >= total) || randomValue(1u, total) <= chance;
27+
return helpers::randomChance(getRandomGenerator(), total, chance);
2928
}
3029

3130
template<typename ContainerT>

0 commit comments

Comments
 (0)