I want to generate what I choose to call “arbitrary” positive floating point numbers; that is, random numbers that are independent of scale (in other words, numbers whose logarithms are evenly distributed). I am not a very mathematician, so as far as I know, there may be a different name for what I need.
Here is my initial naive solution:
import sys import random def arbitrary(min=sys.float_info.min_10_exp, max=sys.float_info.max_10_exp): return 10 ** random.uniform(min, max)
It seems to me that this is probably not ideal: firstly, I believe that there may be some kind of interaction between the limited precision of random.uniform() and the idea of a floating point itself, which will lead to grouping and spaces in the expected output on higher orders.
Is there a better approach? Would it be wiser to just create a string of random bits and then turn that into the floating point number that they represent?
EDIT . As Oli Charlworth noted in the comments, the idea of "converting random bits to float" does not do what I want (which is a uniform distribution of log (n)).
source share