1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-07 17:36:07 +00:00

Fix biased output of randi_range

This commit is contained in:
Yuri Roubinsky
2020-10-29 15:40:53 +03:00
parent 13e93fe904
commit 31faa1f226
4 changed files with 26 additions and 5 deletions

View File

@@ -23,3 +23,13 @@ void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq)
rng->state += initstate;
pcg32_random_r(rng);
}
// Source from https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c
uint32_t pcg32_boundedrand_r(pcg32_random_t *rng, uint32_t bound) {
uint32_t threshold = -bound % bound;
for (;;) {
uint32_t r = pcg32_random_r(rng);
if (r >= threshold)
return r % bound;
}
}