From 35952cf7ad81390e90d550b705f128ca24d9d76d Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Wed, 5 Apr 2023 13:07:02 +0100 Subject: [PATCH] Clamp RandomPercentage to 0...100 --- include/random.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/random.h b/include/random.h index fd340c26d..e7c2a3e28 100644 --- a/include/random.h +++ b/include/random.h @@ -80,8 +80,21 @@ enum RandomTag #define RandomPercentage(tag, t) \ ({ \ - const u8 weights[] = { 100 - t, t }; \ - RandomWeightedArray(tag, 100, ARRAY_COUNT(weights), weights); \ + u32 r; \ + if (t <= 0) \ + { \ + r = FALSE; \ + } \ + else if (t >= 100) \ + { \ + r = TRUE; \ + } \ + else \ + { \ + const u8 weights[] = { 100 - t, t }; \ + r = RandomWeightedArray(tag, 100, ARRAY_COUNT(weights), weights); \ + } \ + r; \ }) #define RandomElement(tag, array) \