testlib icon indicating copy to clipboard operation
testlib copied to clipboard

rnd.next(0ULL, -1ULL) overflow crashes

Open yang-er opened this issue 4 years ago • 1 comments

to = 2^64-1, from = 0, therefore to - from + 1 = 2^64 = 0 for unsigned long long.

Is there any solutions to random in [0, 2^64-1] in one shot?

yang-er avatar Apr 12 '20 14:04 yang-er

By taking a look at testlib.h, unsigned long long next(unsigned long long n) is using long long next(long long n), so basically you can't reach 2^64-1.

To create your rnd.next for unsigned 64bits you can just modify nextBits to operate with unsigned long long all the way instead of long long (seed, multiplier, addend and mask are all unsigned long long so it's not a problem) and accept 64bits... I don't know why they add this limitation

naumazeredo avatar Jun 05 '20 21:06 naumazeredo