Reputation: 25
import random as rd
print([round(rd.random(), 3) for num in range(20)])
So this prints 20 random numbers shortened to 3 decimals.
How would I write the code so that it writes only random numbers higher than 0.4 but lower than or equal to 1?
Any combination of if statements I try yields an error.
Upvotes: 1
Views: 58
Reputation: 5783
Use random.uniform()
.
Here is the documentation.
import random as rd
print([round(rd.uniform(0.4, 1.0), 3) for num in range(20)])
Why do you round the values? You can also use random integers divided by 1000
:
import random as rd
print([rd.randint(401, 1000) / 1000 for num in range(20)])
The lowest you will get is 0.401
the highest is 1.0
Upvotes: 2