Random colors are a small exercise with a surprisingly wide footprint. They show up in test fixtures, data visualization placeholders, avatar generators, and the first 20 minutes of any Python graphics tutorial.
Below are five ways to generate a random color in Python, from the standard library alone to numpy and matplotlib. Each snippet is short enough to read in one screen, and each one returns a different shape of result.
Method 1: the random module
random.randint(0, 255) returns a uniform integer in the closed range 0 to 255. Call it three times and pack the results into a tuple.
import random
def random_rgb():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return (r, g, b)
print(random_rgb())
Output
Method 2: the secrets module
When the color encodes something an attacker should not predict, swap random for secrets. The randbelow(256) call is the same shape. The difference is that secrets draws from a cryptographically secure source.
import secrets
def random_rgb():
r = secrets.randbelow(256)
g = secrets.randbelow(256)
b = secrets.randbelow(256)
return (r, g, b)
print(random_rgb())
Output
Method 3: numpy
numpy.random.randint(0, 256, size=3) returns an array of three integers in one call. Wrap the array with tuple() to drop the numpy type and you have a plain Python tuple.
import numpy as np
def random_rgb():
return tuple(np.random.randint(0, 256, size=3))
print(random_rgb())
Output
(np.int64(15), np.int64(137), np.int64(136))
Method 4: matplotlib named colors
matplotlib keeps a dictionary of CSS4 named colors. Pick one with random.choice and you get a string like ‘aliceblue’ or ‘mediumseagreen’. Use this when you want the color name to be human-readable and the value to land in a CSS context.
import matplotlib.colors as mcolors
import random
def random_named():
return random.choice(list(mcolors.CSS4_COLORS.keys()))
print(random_named())
Output
Method 5: hex strings
Web and graphics pipelines usually want a hex string like ‘#3a7e2c’. Format three random integers with ‘{:02x}’ and concatenate.
import random
def random_hex():
return '#{:02x}{:02x}{:02x}'.format(
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
print(random_hex())
Output
Which method to use
Use the random module for everyday work where the color is decorative. Use the secrets module when the color encodes something an attacker should not guess.
Use numpy when you need to draw many colors in a single call. Use matplotlib when you want a named CSS color. Use a hex string when the consumer is a web template or a CSS file.
Generating many colors at once
All five methods can be wrapped in a list comprehension to draw a palette. Here is the random module version returning ten tuples:
import random
[random.randint(0, 255) for _ in range(30)]
Output
[249, 11, 44, 7, 216, 229, 157, 46, 178, 115, 127, 139, 134, 237, 131, 29, 56, 237, 24, 12, 179, 24, 84, 211, 80, 98, 46, 120, 66, 45]
Conclusion
Random colors in Python are a one-liner, and the right one-liner depends on what shape of result you want. Tuple for an RGB triple, hex string for CSS, named string for readability, numpy array for vectorized code, secrets tuple when predictability is a security concern.
The five snippets above run in any Python 3.10+ environment with the standard library plus optionally numpy and matplotlib. Keep them in a notes file. They will be the answer to the next ‘give me a random color’ question that lands in your inbox.
What is the simplest way to generate a random color in Python?
Use random.randint three times for the red, green, and blue channels. Pack the results into a tuple and you have an RGB color. The whole function fits on three lines.
What is the difference between random and secrets?
random is fast and statistically uniform. secrets is slower and cryptographically secure. Use secrets when the color encodes a token or a session id that an attacker should not predict.
How do I generate a hex color string?
Format three random integers with the format string ‘#{:02x}{:02x}{:02x}’. The 02x specifier pads with a leading zero when the channel value is below 16.
How do I generate many colors at once?
Wrap the per-color call in a list comprehension. For numpy, use numpy.random.randint(0, 256, size=(n, 3)) to draw an entire (n, 3) array in one call.

