3 ms·
If you really want [0,1], it might be more simple to include a check at the start: if (1-in-number-of-floats-in-[0,1)) return 1; Your version of 50/50 spl
by TheLoneWolfling 12y ago
If you really want [0,1], it might be more simple to include a check at the start:
if (1-in-number-of-floats-in-[0,1)) return 1;
Your version of 50/50 split with the mantissa will mean that 0 and 1 will be less often chosen than others.
However, your version won't generate a (quite) uniform distribution anyways, due to subnormals. I think a simple fix for that would be to, if your exponent reaches the subnormals, just return a random mantissa with the subnormal exponent.
- Dylan16807 12y agoI suppose my version might be wrong when it hits a denormal. It would depend on the rounding mode. Also it would never actually get to a denormal. But you're right that to be obviously correct it should stop when it reaches denormals. >Your version of 50/50 split with the mantissa will mean that 0 and 1 will be less often chosen than others. That's intentional, because rounding a real number to the nearest float has the same behavior. The spec is wrong, I advise against following the spec, but that is how you do it.