3 ms·
There's a related article on the site about the spacing of decimal floating-point, which most people would probably find more intuitive: http://www.exploringbi
by userbinator 12y ago
There's a related article on the site about the spacing of decimal floating-point, which most people would probably find more intuitive:
http://www.exploringbinary.com/the-spacing-of-decimal-floating-point-numbers/ http://www.exploringbinary.com/the-spacing-of-decimal-floati...
This spacing is also why generating a random floating-point number the "naive" way will result in a non-uniform distribution.
- onnoonno 12y agoWhat is the naive way? Interpreting a random bit string as a float? Or rather randint(1000000000L)/1.0e9?
- emilga 12y agoThe naive way is presumably to just throw all the floats in a hat, mix them up, and pull one out. This is the method you described. The problem here is that the floats clump together around zero, and they spread out further away from zero. So the hat-method is biased towards smaller numbers. A possible better method -- this is just an educated guess -- might be to divide your interval in half, pick the left or right half randomly, and then divide the resulting interval in half, etc. until your interval contains only one float. I'm not sure how efficient this would be to implement, or if there are any edge-cases to consider, but it would at least avoid the inherently biased distribution of the floats.
- onnoonno 12y agoHmm, but the randint(...)/... method should come pretty close to a uniform distribution on the 0..1 interval, shouldn't it? It wouldn't have maximum entropy, though.