How to create a random two point floating point number in python

I want to create a two-dot random floating point number. For example: 2.54 How to change uniform(a,b)

to python. Thanks to

+3


source to share


3 answers


You can use a round function with a uniform function to limit a floating point 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

      

+7


source


This might be a possible solution. As requested by @Damian



>>> from random import randint
>>> randint(100, 999)/100.00
7.32
>>> randint(100, 999)/100.00
4.69
>>> randint(100, 999)/100.00
5.36

      

+2


source


If you want to create a random number between two numbers, with a certain number of decimal places, here's a way:

import random

greaterThan = float(1)
lessThan = float(4)
digits = int(2)

rounded_number = round(random.uniform(greaterThan, lessThan), digits)

      

in this case your random number will be between 1 and 4, with two digits

+1


source







All Articles