is it possible to find random floats in range [a,b] in python?

后端 未结 5 2090
旧巷少年郎
旧巷少年郎 2021-01-06 17:07

I\'m trying to generate in python random floats in the range of [0.8,0.9] , but unfortanetly all the tools i found could only generate randoms in the range of [a,b) for floa

5条回答
  •  别那么骄傲
    2021-01-06 17:41

    Quoting the random.uniform() docstring:

    Get a random number in the range [a, b) or [a, b] depending on rounding.

    So you don't even know if the endpoint is included or not. But you should not care either -- there are 900719925474100 floating point numbers in the range [0.8, 0.9]. It will take a lifetime until a single one of them occurs anyway, so there is no practical difference to including the endpoint or not, even if the endpoint can be represented exactly as a floating point number.

提交回复
热议问题