From 275d5acc9a15a09ed5d8334beac54759ac1e8bea Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 22 May 2026 23:16:29 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20distribution=20test=20for?= =?UTF-8?q?=20pow2=5Fweighted=5Froll?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds `test_pow2_weighted_roll_distribution` to `tests/test1.cpp` to verify that `pow2_weighted_roll` correctly generates outcomes following a power-of-two weighted distribution. The test operates by performing a large number of rolls and comparing the observed frequencies against the theoretically expected probability distribution, thereby ensuring that `pow2_weighted_roll` works exactly as expected without causing regressions. Co-authored-by: perim <436583+perim@users.noreply.github.com> --- tests/test1.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test1.cpp b/tests/test1.cpp index 5fdac30..3273588 100644 --- a/tests/test1.cpp +++ b/tests/test1.cpp @@ -285,6 +285,37 @@ static void test_filtered_const_roll_table_3() assert(results[1] > n * 87 / 100 && results[1] < n * 93 / 100); } +static void test_pow2_weighted_roll_distribution() +{ + seed s(42); + const int high = 4; + const int total_rolls = 1000000; + std::vector counts(high + 1, 0); + + for (int i = 0; i < total_rolls; ++i) + { + int result = s.pow2_weighted_roll(high); + assert(result >= 0 && result <= high); + counts[result]++; + } + + uint64_t n = 1ULL << (high + 2); + uint64_t total_possibilities = n - 2; + + for (int i = 0; i <= high; ++i) + { + uint64_t weight = 1ULL << (high + 1 - i); + double expected_prob = (double)weight / total_possibilities; + int expected_count = (int)(total_rolls * expected_prob); + + int margin = expected_count / 10; // 10% margin of error + if (margin < 50) margin = 50; // minimum margin + + assert(counts[i] >= expected_count - margin); + assert(counts[i] <= expected_count + margin); + } +} + static void test_dice_guards() { seed s(123); @@ -340,6 +371,7 @@ int main(int argc, char **argv) test_linear_series_3(); linear_roll_table_test(); edge_cases(); + test_pow2_weighted_roll_distribution(); test_dice_guards(); int j = 0;