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,1.0)
, but it returns a value >= 0.1 and < 1.0, we can't use this, because our search includes also 1.0.
Does somebody else have an idea for this problem?
random. random() is what you are looking for: From python docs: random. random() Return the next random floating point number in the range [0.0, 1.0).
Use randint() Generate random integer randint() function to get a random integer number from the inclusive range. For example, random. randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10].
The Python uniform() function takes two parameters: the lower limit and the upper limit, and returns a random number between the range. The lower limit is inclusive, but the upper limit is exclusive.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With