1

I have two float numbers where both have one number after the decimal point. I want to generate a a random float where only the first number after the decimal point count .

Example:

Let's say we have two numbers : 3.5 and 3.8, I want a way to generate a random number like 3.6 and 3.7, I don't want to generate numbers like 3.58 or 3.584 and so one, I want a random float number .

I can't use random.uniform(a,b) because it doesn't do what I want, it can generate random numbers for other numbers after decimal point (second and third number after decimal point like 3.589)

martineau
  • 119,623
  • 25
  • 170
  • 301
Anis Souames
  • 334
  • 1
  • 4
  • 13
  • round(random.uniform(3.5, 3.8), 1) – petruz Jan 17 '17 at 13:14
  • 1
    Linked duplicate has the answer to generate the random number between two floats. Refer [*Limiting floats to two decimal points*](http://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) for limiting the decimal point of the randomly generated float – Moinuddin Quadri Jan 17 '17 at 13:16

3 Answers3

4

Why not using random.randint on boundaries times 10 and divide afterwards?

>>> a=3.5
>>> b=3.8
>>> random.randint(a*10,b*10)/10
3.8
>>> random.randint(a*10,b*10)/10
3.6

and if you want to omit a and b:

random.randrange(a*10+1,b*10)/10
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

Is this what you are looking for?

 round(random.random()*10,1)

Just round it to the required number of places.

e4c5
  • 52,766
  • 11
  • 101
  • 134
2

I don't see why this shouldn't work for you.

a=3.5
b=3.8

round(random.uniform(a,b),1)
Anomitra
  • 1,111
  • 15
  • 31