Use the standard library’s random module for simulations and general-purpose randomness, and switch to the secrets module when you need cryptographic strength. The steps below show reliable ways to produce integers, floats, secure values, and command-line outputs.
Method 1: Generate a random integer (general-purpose)
import random
n = random.randint(0, 100) # 0 ≤ n ≤ 100
print(n)
This produces uniformly distributed integers over the closed interval you specify. For stride/step logic (for example, even numbers only), use random.randrange(start, stop, step).
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Method 2: Generate a random float
import random
x = random.random() # 0.0 ≤ x < 1.0
print(x)
y = random.uniform(2.5, 10.0) # a ≤ y ≤ b (end inclusion may vary due to floating-point rounding)
print(y)
Use random.random() for quick normalized values, and random.uniform() when you need a specific numeric span.
Method 3: Generate a cryptographically secure random integer
import secrets
token = secrets.randbelow(10) # 0..9, suitable for security-sensitive uses
print(token)
Use secrets for passwords, tokens, or any security-related randomness. The random module is deterministic by design and is not suitable for cryptographic purposes.
Method 4: Reproduce results by seeding
import random
random.seed(42)
print(random.randint(0, 100))
print(random.random())
Seeding stabilizes your outputs for testing and demonstrations. Accepted seed types include None, int, float, str, bytes, and bytearray.
Method 5: Pick random items from a sequence
import random
colors = ["red", "green", "blue"]
print(random.choice(colors))
print(random.choices(colors, k=5))
print(random.sample(colors, k=2))
Use weights (via choices(..., weights=[...])) when you need non-uniform probabilities.
Method 6: Use the Python 3.13+ command-line interface
python -m random 10
# or
python -m random --integer 10
python -m random --float 1.8
python -m random --choice egg bacon sausage spam
This built-in CLI streamlines quick draws without writing a script and is available starting in Python 3.13.
Method 7 (optional): Generate many random numbers efficiently (NumPy)
import numpy as np
# 15 integers in [0, 10)
ints = np.random.randint(0, 10, size=15)
# 5 floats in [0.0, 1.0)
floats = np.random.random(5)
print(ints)
print(floats)
NumPy’s random routines generate large batches quickly and integrate well with scientific and data workflows.
Tips and cautions
- For security-sensitive randomness (passwords, tokens), always use
secrets, notrandom. random.random()is half-open: it never returns 1.0.random.uniform(a, b)computes via floating-point arithmetic—due to rounding, the endpointbmay or may not appear.- Seed the generator with
random.seed()to make tests reproducible.
With these approaches, you can generate integers, floats, reproducible sequences, secure tokens, and even command-line outputs efficiently and correctly.






