Hey folks! In this tutorial, we’ll look at how to generate random colors in Python. We’ll create colors in two different forms. Python modules like Numpy, Matplotlib, and turtle can be used to produce color.
Using random() function to generate random colors
import random
for i in range(3):
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
rgb = [r,g,b]
print('A Random color is :',rgb)
To begin, import the random function in Python to obtain a random color. The variable r stands for red, g stands for green, and b stands for blue. We already know that the RGB format contains an integer value ranging from 0 to 255.
As a result, we’ve set the range to 0 to 255. It will use any value in the range. random.randint() is a method for determining the range.
A Random color is : [222, 169, 158]
A Random color is : [66, 19, 84]
A Random color is : [157, 146, 62]
Using Numpy Module to generate random colors
import numpy as np
for i in range(3):
random_color=list(np.random.choice(range(255),size=3))
print("A Random color is: ",random_color)
The color is then assigned a value and size in the variable random color. Because we started it as a list, the color will appear in the list—next, print random color.
A Random color is: [241, 4, 161]
A Random color is: [96, 48, 224]
A Random color is: [228, 20, 55]
Using Matplotlib Library to Generate Random Colors
import matplotlib.pyplot as plt
import random
no_of_colors=5
color=["#"+''.join([random.choice('0123456789ABCDEF') for i in range(6)])
for j in range(no_of_colors)]
print(color)
for j in range(no_of_colors):
plt.scatter(random.randint(0,10),random.randint(0,10),c=color[j],s=200)
plt.show()
In a variable called no of colors, assign a value. The # and color code were then joined using the join() method. The color code will always begin with #. To iterate, use a for a loop. The color code is now produced.
Because we started it as a list, the color will appear in the list—next, print random color.

Conclusion
Congratulations! You just learned how to generate random colors in different ways. Hope you enjoyed it! 😇
Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:
Thank you for taking your time out! Hope you learned something new!! 😄