pythonrandomfloating-point

How to generate a floating point random number with two precision in Python


I want to generate a floating point random number with two precision.

For example: 2.54

How to change the uniform(a, b) in Python.


Solution

  • You can use round function with uniform function to limit float number to two decimal places.

    Example:

     round(random.uniform(1.5, 1.9),2)
     Out[]: 1.62
    
     round(random.uniform(1.5, 1.9),3)
     Out[]: 1.885