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

@ -58,12 +58,18 @@ public:
_FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); }
_FORCE_INLINE_ int randi_range(int from, int to) {
unsigned int ret = randbase.rand();
if (to < from) {
return ret % (from - to + 1) + to;
} else {
return ret % (to - from + 1) + from;
int range;
int min;
if (to > from) {
range = to - from + 1;
min = from;
} else if (to < from) {
range = from - to + 1;
min = to;
} else { // from == to
return from;
}
return randbase.rand(range) + min;
}
RandomNumberGenerator() {}