Generate Random RGB Colors in Python

Colors are represented in several formats in the digital world. The RGB and Hexadecimal formats are just two of the most popular. This lesson will discuss how to generate random RGB colors in Python. We shall construct a random code that can represent color when we talk about producing random colors. Color codes will be generated in a variety of forms using various approaches. Now we’ll look at how to use Python to generate random colors. Colors will be generated in two formats. The first is RGB, and the second is hexadecimal. We use those two forms a lot in the digital world. Colors are typically portrayed in a variety of ways. Numpy, Matplotlib, and turtle are examples of Python libraries that can be used to generate a color.

The RGB color system’s three primary hues are red, green, and blue. Its value ranges from 0 to 255. These three colors can be combined to produce different colors. In the hexadecimal format, the #symbol is followed by six hexadecimal numbers. RGB colors are hexadecimal colors. To acquire a random color, the random() method is used. Random() is a commonly used Python module that can produce random integers or colors. It’s helpful to pick things at random and shuffle the items in the list.

Example 1:

To get the random colors, first import a random module. After that, you’ll need to make a for loop that iterates 20 times to acquire 20 different colors. The join() function is then used to join the # and color code. The Python String join() function is a string technique that returns a string with the str separator used to connect the sequence’s components. The letter # is utilized to start the color code. To iterate, use the for loop. The color codes have now been created.

import random
for j in range(20):
    colors = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]

Below you can see that the color codes are successfully generated.

Example 2:

In this instance, we will talk about the hexadecimal number system. A 16-character, also known as a symbol numbering system, is the hexadecimal number system. It’s also known as hex in the Python language. This example will use a random module to generate a random color in hexadecimal format. The join() function is being utilized to link the # and color code. The letter # will be used to start the color code. To iterate, use the for loop. The color code has now been produced. There are many additional color formats to choose from, and converting between them is simple. One thing to keep in mind is that too generated color codes in a variety of forms can be done. To view these colors, you have to use additional modules to create a visual or plot a graph.

The output shows that the color is produced following the code above.

Example 3:

Here we will show you how to generate random colors with the help of the matplotlib library. Matplotlib is a data imagining and graphical scheming package for Python and its arithmetical extension NumPy that runs on all platforms. As a result, it provides an open-source alternative to MATLAB. Matplotlib’s APIs can also be used to embed plots in graphical user interfaces. Importing the matplotlib library is the first step. After that, you’ll need to ingress a random module. Next, put a value in the no of colors variable.

The join() function is then used to link the # and color code. The letter # will always be used to start the color code. To iterate, use the for loop. The color code has now been produced. The color will be available in the search because we decided to declare it as a list—next, print a random color. Other than these methods, the turtle module can also be used for making random colors in Python. Turtle is a module in Python that comes pre-installed that enables users to apply a virtual canvas to create graphics and shapes. It is a reimplementation of the same-named module included in the Python standard package until Python 2.5. The turtle is the name of the onscreen pen that you use to draw with, and it is also the name of the library.

import matplotlib.pyplot as pt
import random
color=["#"+''.join([random.choice('0123456789ABCDEF') for i in range(6)])
       for j in range(colors)]
for j in range(colors):

The above code generated this output.


In all browsers, RGB color values are supported. The RGB color code is used to specify an RGB color value (red, green, blue). Each of the three parameters (red, green, and blue) specifies the color’s intensity as a number between 0 and 255. This lesson covered how to generate random RGB colors in Python. To generate colors, we used a variety of methods and formats. These are the ways for generating random colors that are accessible.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content