Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random number between 0.1 and 1.0. Python

Tags:

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?

like image 559
user2320923 Avatar asked Apr 29 '13 21:04

user2320923


People also ask

How do you generate a random value between 0 and 1 in Python?

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).

How do you generate a random number between 1 and 2 in Python?

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].

Which function returns a random number between 0.0 and 1.0 where 0.0 is inclusive but 1.0 is exclusive in Python?

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.


1 Answers

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.

like image 186
Elmar Peise Avatar answered Jan 06 '23 03:01

Elmar Peise