You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
thrust::uniform_int_distribution<uint64_t> exclusively produces multiples of 4096 #758
The thrust::uniform_int_distribution uses uniform_real_distribution under the hood.
However, for 64 bit numbers this means the lower 12 bits are always 0.
Sounds like a casting issue somewhere in the implemenation-- a 64-bit float has 52 bits of integer precision, leaving 12 bits for sign / exponent information, so that sounds relevent.
For the record the code above above produces 16bits/4 zeros (not 12bits, 3 zeros) at the end because the 48bit engine (ranlux48). Even if the bug wasn't there the 4th to last digit would still be 0. Output would be in that form:
@djns99 though is still right that thrust::uniform_int_distribution is still bogus due to its reliance on uniform_real_distribution. Exactly as Allison explained it's because of the mantissa being only 52 bits:
64bit integer input: 64-52 = 12 bits lost in conversion
When we convert back to integer (shift left 12 bits) after the random distr computation, these 12 bits are 0s.
For the record the code above above produces 16bits/4 zeros (not 12bits, 3 zeros) at the end because the 48bit engine (ranlux48). Even if the bug wasn't there the 4th to last digit would still be 0.
This seems like there is also a corresponding bug in the uniform_real_distribution I guess this comes down to a question of what thrust's goals are. If we are trying to provide robust random utilities, then this needs fixed as well.
Some statistical methods can be quite sensitive to systematic biases like these, and generally the point of using the library types is to get good distribution without needing to research it yourself.
I vote to refactor both the uniform_real_distribution and uniform_int_distribution methods to match libstdc++ perhaps.
e.g. this std::generate_canonical function appears to be what's missing for real distribution.
For uniform_int_distribution we should be able to use the libstdc++ algorithm. I believe this is standard as we have seen it in a couple of places
Activity
alliepiper commentedon Jan 4, 2021
Good catch, thanks for reporting this.
Sounds like a casting issue somewhere in the implemenation-- a 64-bit float has 52 bits of integer precision, leaving 12 bits for sign / exponent information, so that sounds relevent.
gonidelis commentedon Apr 9, 2025
For the record the code above above produces 16bits/4 zeros (not 12bits, 3 zeros) at the end because the 48bit engine (
ranlux48). Even if the bug wasn't there the 4th to last digit would still be0. Output would be in that form:@djns99 though is still right that
thrust::uniform_int_distributionis still bogus due to its reliance onuniform_real_distribution. Exactly as Allison explained it's because of the mantissa being only52 bits:When we convert back to integer (shift left 12 bits) after the random distr computation, these 12 bits are
0s.djns99 commentedon Apr 10, 2025
This seems like there is also a corresponding bug in the
uniform_real_distributionI guess this comes down to a question of what thrust's goals are. If we are trying to provide robust random utilities, then this needs fixed as well.Some statistical methods can be quite sensitive to systematic biases like these, and generally the point of using the library types is to get good distribution without needing to research it yourself.
I vote to refactor both the
uniform_real_distributionanduniform_int_distributionmethods to match libstdc++ perhaps.e.g. this
std::generate_canonicalfunction appears to be what's missing for real distribution.For
uniform_int_distributionwe should be able to use the libstdc++ algorithm. I believe this is standard as we have seen it in a couple of places