开发者

How do you generate a random double between these points?

开发者 https://www.devze.com 2023-01-22 07:24 出处:网络
37.807614 to 37.786996 The rando开发者_运维知识库mly generated double must have the same precision (num of digits) as those above.
37.807614 to 37.786996 

The rando开发者_运维知识库mly 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号