diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2020-12-22 11:28:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-22 11:28:59 +0100 |
commit | 30d469a5e0f70860f3c4ce4508d6564ca389320b (patch) | |
tree | 759a4778cacf96d20832e05086c5e5d05c5bfb6b /tests/test_random_number_generator.h | |
parent | 6e43c68e40b81005d0c393eced6cf871d1964fbd (diff) | |
parent | 900e55eb70febc4855ad40b95574ac838e6ca234 (diff) |
Merge pull request #44562 from mcognetta/randi_range_simplification
Simplify `randi_range` implementation
Diffstat (limited to 'tests/test_random_number_generator.h')
-rw-r--r-- | tests/test_random_number_generator.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/test_random_number_generator.h b/tests/test_random_number_generator.h index 900bb55ffd..999e6d4862 100644 --- a/tests/test_random_number_generator.h +++ b/tests/test_random_number_generator.h @@ -250,6 +250,26 @@ TEST_CASE("[RandomNumberGenerator] Restore from seed") { CHECK_MESSAGE(s0_1_before == s0_1_after, msg); CHECK_MESSAGE(s0_2_before == s0_2_after, msg); } + +TEST_CASE_MAY_FAIL("[RandomNumberGenerator] randi_range bias check") { + int zeros = 0; + int ones = 0; + Ref<RandomNumberGenerator> rng = memnew(RandomNumberGenerator); + for (int i = 0; i < 10000; i++) { + int val = rng->randi_range(0, 1); + val == 0 ? zeros++ : ones++; + } + CHECK_MESSAGE(abs(zeros * 1.0 / ones - 1.0) < 0.1, "The ratio of zeros to ones should be nearly 1"); + + int vals[10] = { 0 }; + for (int i = 0; i < 1000000; i++) { + vals[rng->randi_range(0, 9)]++; + } + + for (int i = 0; i < 10; i++) { + CHECK_MESSAGE(abs(vals[i] / 1000000.0 - 0.1) < 0.01, "Each element should appear roughly 10% of the time"); + } +} } // namespace TestRandomNumberGenerator #endif // TEST_RANDOM_NUMBER_GENERATOR_H |