diff options
| author | Yuri Roubinsky <chaosus89@gmail.com> | 2020-10-29 15:40:53 +0300 |
|---|---|---|
| committer | Yuri Roubinsky <chaosus89@gmail.com> | 2020-10-29 15:41:00 +0300 |
| commit | 31faa1f22626b8745ed4a1541497832b6f595df2 (patch) | |
| tree | 25baec4470e850e40c1ab5099ff8d9886c6087de /thirdparty/misc/pcg.cpp | |
| parent | 13e93fe90499b3aac569ac09440c8d6056efba68 (diff) | |
| download | redot-engine-31faa1f22626b8745ed4a1541497832b6f595df2.tar.gz | |
Fix biased output of randi_range
Diffstat (limited to 'thirdparty/misc/pcg.cpp')
| -rw-r--r-- | thirdparty/misc/pcg.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/thirdparty/misc/pcg.cpp b/thirdparty/misc/pcg.cpp index c421e16f89..5f4bf40460 100644 --- a/thirdparty/misc/pcg.cpp +++ b/thirdparty/misc/pcg.cpp @@ -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; + } +} |
