views:

79

answers:

2
37.807614 to 37.786996 

The randomly generated double must have the same precision (num of digits) as those above.

For example, 37.792242 would be good, whereas 37.7823423425 would be bad.

+1  A: 
round(random.uniform(num1, num2), 6)

You will occasionally get numbers that end with ...0001 or ...9999 or so due to IEEE 754 inaccuracies though.

Ignacio Vazquez-Abrams
Bear in mind that this problem is subject to floating-point arithmetic problems as described in http://docs.python.org/tutorial/floatingpoint.html?highlight=truncate
Rafe Kettler
This gets the rounding wrong at the boundaries, causing biased results. round(random.uniform(0, 0.000010), 6)` will give 0.000000 and 0.000010 half as frequently as each value between it; the range of values that round to 0, [0,0.5), is half the size of the range of values that round to 0.000001, [0.0000005,0.0000015).
Glenn Maynard
@Glenn: So then what's the correction factor that needs to be added to each endpoint?
Ignacio Vazquez-Abrams
I don't feel like sample-testing it to be sure, but probably (.1**6)/2, where 6 is the precision.
Glenn Maynard
A: 

Not sure if I'm missing something, but to avoid rounding, and since you want exactly that precision, isn't it easiest to work in integers and then convert to floating point by division? I.e.

offset = float(random.randint(0,20618)) num = (offset + 37786996.0) / 1000000.0

Peter van Heusden