4 ms·
I was curious why that'd be the case. I checked GCC's implementation. It seems to be here: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/includ
by gnulinux 6y ago
I was curious why that'd be the case. I checked GCC's implementation. It seems to be here: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/uniform_int_dist.h#L238 https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-...
Looks like they had to make this very generic, generic enough to account for random number generators that return value in specific ranges. If you have a generator that guarantees to return a uniformly random 32 bit value, it seems much faster to do (r%(max-min)+min) instead.
- brandmeyer 6y agoTaking the modulus of the desired range isn't uniform.
- gnulinux 6y agoThat's true, and obviously so but I never realized it until now. I'm curious why r%(max-min)+min is widespread "common" way of doing this, maybe I just saw it in a few places and never questioned it.