Skip to content

Commit

Permalink
Merge pull request godotengine#44562 from mcognetta/randi_range_simpl…
Browse files Browse the repository at this point in the history
…ification

Simplify `randi_range` implementation
  • Loading branch information
akien-mga authored Dec 22, 2020
2 parents 6e43c68 + 900e55e commit 30d469a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
12 changes: 2 additions & 10 deletions core/math/random_pcg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,8 @@ float RandomPCG::random(float p_from, float p_to) {
}

int RandomPCG::random(int p_from, int p_to) {
int range;
int min;
if (p_to > p_from) {
range = p_to - p_from + 1;
min = p_from;
} else if (p_to < p_from) {
range = p_from - p_to + 1;
min = p_to;
} else { // from == to
if (p_from == p_to) {
return p_from;
}
return rand(range) + min;
return rand(abs(p_from - p_to) + 1) + MIN(p_from, p_to);
}
20 changes: 20 additions & 0 deletions tests/test_random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 30d469a

Please sign in to comment.