Generating random colors in Python is a good exercise if you’re just starting out. It is recommended for beginners to learn how to generate random colors in different ways as it involves applying a lot of basic concepts like variables, functions, loops and modules. It helps the developer learn programming logic, problem-solving and algorithms.
In the world of programming, generating random colors opens up endless possibilities for creating beautiful graphics. You can combine them with modules like Turtle to create attractive designs with just a few lines of code to level up your Python Skills.
In this tutorial, we’ll learn how to generate random colors using several methods from easy to advanced. Let’s get started.
Random Color Generator
A random color generator or a random color picker in Python is basically a function that generates number of colors by randomly selecting integer values for the red, green, and blue (RGB) channels. It usually returns an actual color or RGB value as a tuple.
Let us now look at the different ways to generate random colors in Python
Using the random module
The random module is a built-in module, that has a method randint() which can produce integers within the range passed as arguments. We can implement it to generate random colors.
import random def random_color_generator(): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) return (r, g, b) random_color = random_color_generator() print(random_color)
Here we have created a function random_color_generator() to get the random color. The function contains randint(0, 255) to obtain integers within the range 0 to 255 for each channel, then returns them as a tuple.
(83, 236, 226)
Using the secrets module
This module has a randbelow() function for generating random values within a specified range. The working of this function is the same as the previous one, but it is a more secure way.
import secrets def random_color_generator(): r = secrets.randbelow(256) g = secrets.randbelow(256) b = secrets.randbelow(256) return (r, g, b) random_color = random_color_generator() print(random_color)
Here also the return value is a tuple containing the RGB value.
(29, 74, 161)
Using the numpy module
There is a more advanced and faster way to generate random colors using the numpy module. It’s very simple and requires only a few lines of code. Let’s see how.
import numpy as np def random_color_generator(): color = np.random.randint(0, 256, size=3) return tuple(color) random_color = random_color_generator() print(random_color)
Here we have used np.random.randint() function and passed range as arguments to produce the random integer between them, moreover, we have passed 3 as the last argument which indicates that we want to get an array containing three randomly generated integers. Next, we can convert that array to a tuple using tuple() and then return it to the caller.
(88, 234, 58)
Also Read: An Introduction to NumPy Arrays
Using the matplotlib module
We can use the mcolors.CSS4_COLORS.keys() method of this module to get a list of CSS4 different colors and then use the random.choice() function of the random module to choose one of them randomly to display in the console. In this way, we can get different random colors.
import matplotlib.colors as mcolors import random def random_color_generator(): color = random.choice(list(mcolors.CSS4_COLORS.keys())) return color random_color = random_color_generator() print(random_color)
Here the return value is a string containing the name of a randomly selected color.
Note: If you want to get the code for the color, you can use mcolors.CSS4_COLORS.values() instead.
Through this tutorial, we have learned many new easy-to-use methods to generate random colors. You can try them on your own and use Python core concepts to eliminate unnecessary code for more optimizations, such as using a loop instead of three identical lines to get a random integer each time.
In conclusion, we can say that all methods can generate random colors, but if you want the easiest way, it is random() function of random module, and if your need is to get colors instead of RGB values, the best way is to use the matplotlib module. In the end, hope this tutorial provided you with enough information to be able to generate random colors.