Numbers and randomness
Choose numeric and random tools based on the required guarantees.
On this page
The concept
Binary floating-point cannot represent every decimal fraction exactly. This explains the familiar small discrepancy in 0.1 + 0.2. math.isclose compares approximate numeric results using a tolerance policy. Decimal constructed from decimal text represents those decimal inputs directly.
random is suitable for simulations, games, and sampling. A local Random instance with a fixed seed supports repeatable demonstrations. It is not suitable for security tokens; use secrets for security-sensitive randomness. Financial calculations also need explicit currency and rounding rules, beyond merely choosing Decimal.
Worked example
import math
import random
from decimal import Decimal
print(math.sqrt(64), math.ceil(2.1), math.floor(2.9))
print(0.1 + 0.2)
print(math.isclose(0.1 + 0.2, 0.3))
print(Decimal("0.1") + Decimal("0.2"))
rng = random.Random(42)
print("Die roll:", rng.randint(1, 6))
print("Choice:", rng.choice(["apple", "banana", "pomegranate"]))
items = [1, 2, 3, 4]
rng.shuffle(items)
print(items)Trace the result
- Compare the displayed float sum with the Decimal sum.
- isclose expresses approximate comparison rather than exact equality.
- The seeded random generator produces a die roll, a choice, and an in-place shuffle.
8.0 3 2
0.30000000000000004
True
0.3
Die roll: 6
Choice: apple
[4, 2, 3, 1]Line-by-line walkthrough
Line 1: import math
Load math (mathematical operations and finite-number checks). Importing makes these names available; it is not a call to every function in the module.
Line 2: import random
Load random (repeatable simulation-oriented random values). Importing makes these names available; it is not a call to every function in the module.
Line 3: from decimal import Decimal
Import Decimal from decimal. This supplies explicit decimal arithmetic. The caller can now use the imported names directly.
Line 5: print(math.sqrt(64), math.ceil(2.1), math.floor(2.9))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 6: print(0.1 + 0.2)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: print(math.isclose(0.1 + 0.2, 0.3))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: print(Decimal("0.1") + Decimal("0.2"))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 9: rng = random.Random(42)
Evaluate random.Random(42), then bind or update rng with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Line 10: print("Die roll:", rng.randint(1, 6))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 11: print("Choice:", rng.choice(["apple", "banana", "pomegranate"]))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 12: items = [1, 2, 3, 4]
Evaluate [1, 2, 3, 4], then bind or update items with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 13: rng.shuffle(items)
Call rng.shuffle with the displayed arguments. This call may have an effect even when its return value is not assigned.
Line 14: print(items)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Decimal(0.1) starts from an already approximated float. Use Decimal("0.1") when the source is decimal text.