Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - random float with limited decimal digits

I just found out how to make random numbers in Python, but if I print them out, they all have 15 decimal digits. How do I fix that? Here is my code:

import random
import os

greaterThan = float(input("Your number will be greater than: "))
lessThan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))

os.system('cls')

if digits == 15:
    print(random.uniform(greaterThan, lessThan))

if digits == 14:
    print(random.uniform(greaterThan, lessThan))

if digits == 13:
    print(random.uniform(greaterThan, lessThan))

if digits == 12:
    print(random.uniform(greaterThan, lessThan))

if digits == 11:
    print(random.uniform(greaterThan, lessThan))

if digits == 10:
    print(random.uniform(greaterThan, lessThan))

*(this just continues down to 0)

I know you can do like print("%.2" % someVariable) but the random numbers Python creates with this method are not saved in any variables. At least I think so. I would also like to know if there is a way of letting a variable choose the amount of decimal points, like print("%." + digits % anotherVariable) but I tried tha out and of course it failed.

I really got no idea. Hopefully you can help. Thanks.

like image 750
SALZKARTOFFEEEL Avatar asked Jul 23 '15 18:07

SALZKARTOFFEEEL


People also ask

Is there a limit to the floating point numbers that Python can handle?

Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 x 10308.


3 Answers

If you want to take whatever number you get from random.uniform and truncate it to the specific number of digits you can use the round() function.

It allows you to round to a specific precision. For example:

import random

greaterThan = float(input("Your number will be greater than: "))
lessThan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))

rounded_number = round(random.uniform(greaterThan, lessThan), digits)
print(rounded_number)
like image 176
Morgan Thrapp Avatar answered Oct 19 '22 03:10

Morgan Thrapp


You can also handle float precision using .format():

print float("{0:.2f}".format(random.uniform(greaterThan, lessThan)))

Where the 2 can be replaced by how many digits after decimal you want.

like image 28
kennes Avatar answered Oct 19 '22 02:10

kennes


I'm not sure what you mean by "fixing it", since what you're talking about is pretty fundamental to how computers perform what's called floating-point arithmetic.

However, that said, I am pretty sure you are looking for the decimal module:

import decimal

Best of luck!

Edit:

Taking a second look at your post, this feels wonky. Is this a homework assignment? Why have the digits variable at all, if you're not doing anything with its value? Sure, you check it (over and over and over again), but you're doing the same thing each time.

If you're actually doing something differently for each value of digits, then you should use if-elif-else:

if digits == 15:
    do_stuff()
    ...
elif digits == 14:
    do_other_stuff()
    ...
elif digits == 13:
    do_even_moar_different()
    ...
...
else:
    and_now_for_something_completely_different()

But that's ugly, and Python is supposed to be pretty (import this).

if digits > some_value:
    do_stuff()
    ...
elif digits <= some_other_value:
    do_something_else()
...

I do encourage you to read the floating-point arithmetic link, since this is very important to understand, at least a little.

like image 1
Andy Kubiak Avatar answered Oct 19 '22 02:10

Andy Kubiak