summaryrefslogtreecommitdiff
path: root/thirdparty
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-10-30 15:00:44 +0100
committerGitHub <noreply@github.com>2020-10-30 15:00:44 +0100
commitf98db723b91feb5ffc54460813be2c712e0b8ff8 (patch)
tree0f35f28b6a3f7cd2578cd8af320122a952030fe5 /thirdparty
parentb1ed10da1b5452486cd745964672f8017404eaeb (diff)
parent31faa1f22626b8745ed4a1541497832b6f595df2 (diff)
Merge pull request #43184 from Chaosus/fix_randi_range_biased
Fix biased output of randi_range
Diffstat (limited to 'thirdparty')
-rw-r--r--thirdparty/misc/pcg.cpp10
-rw-r--r--thirdparty/misc/pcg.h1
2 files changed, 11 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;
+ }
+}
diff --git a/thirdparty/misc/pcg.h b/thirdparty/misc/pcg.h
index 6f42b3b094..0faab73e64 100644
--- a/thirdparty/misc/pcg.h
+++ b/thirdparty/misc/pcg.h
@@ -11,5 +11,6 @@
typedef struct { uint64_t state; uint64_t inc; } pcg32_random_t;
uint32_t pcg32_random_r(pcg32_random_t* rng);
void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq);
+uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound);
#endif // RANDOM_H