Issue Description
As mentioned in b93d569#r43494808, the changes to EncodeEntropyRand in #7 cause the random component to be 0 when the function is called on a ulid. The problem occurs with how casting is happening here:
inline void EncodeEntropyRand(ULID& ulid) {
ulid.data[6] = (uint8_t)(std::rand() * 255ull) / RAND_MAX;
In C++ order of operations, a c style cast happens before multiplication or division. That means this expression is resolved:
then cast as a uint8_t before the division by RAND_MAX occurs. Thus, you end up with a number always less than 0 (since a unint8_t can only hold up to 255, and RAND_MAX (on 32 bit platforms) is 2147483647).
Fix
A fix was proposed in #9. However, I think that only capturing the last 0xFF part of the output of rand() may be suboptimal (see std::rand() notes here).
Another fix would be to force the correct order of operations and cast the entire expression. I've tested that this is working.
Issue Description
As mentioned in b93d569#r43494808, the changes to
EncodeEntropyRandin #7 cause the random component to be 0 when the function is called on a ulid. The problem occurs with how casting is happening here:In C++ order of operations, a c style cast happens before multiplication or division. That means this expression is resolved:
then cast as a uint8_t before the division by
RAND_MAXoccurs. Thus, you end up with a number always less than 0 (since a unint8_t can only hold up to 255, andRAND_MAX(on 32 bit platforms) is 2147483647).Fix
A fix was proposed in #9. However, I think that only capturing the last
0xFFpart of the output ofrand()may be suboptimal (see std::rand() notes here).Another fix would be to force the correct order of operations and cast the entire expression. I've tested that this is working.