Generate random number between 0.1 and 1.0. Python

后端 未结 10 1893
南方客
南方客 2020-12-29 05:23

I\'m trying to generate a random number between 0.1 and 1.0. We can\'t use rand.randint because it returns integers. We have also tried random.uniform(0.1

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 05:51

    How "accurate" do you want your random numbers? If you're happy with, say, 10 decimal digits, you can just round random.uniform(0.1, 1.0) to 10 digits. That way you will include both 0.1 and 1.0:

    round(random.uniform(0.1, 1.0), 10)
    

    To be precise, 0.1 and 1.0 will have only half of the probability compared to any other number in between and, of course, you loose all random numbers that differ only after 10 digits.

提交回复
热议问题