# 7 Ways to Generate Random Color in Python

Now we are going to see how to generate random colors using Python. We are going to generate colors in two formats. One is RGB format, and the other is hexadecimal format. In this digital world, we are using that two formats highly. Generally, colors are represented in different formats. We can generate color using python libraries such as Numpy, Matplotlib, and turtle.

RGB represents Red, Green, and Blue. It has an integer value from 0 to 255. The combination of these three colors gives many colors. The hexadecimal format starts with the #sign, followed by six hexadecimal digits. The hexadecimal colors are Red, Green, and Blue. We are using a random() module to generate a random color.

Contents

## What is a random() Module?

random() is a module that is useful to generate random integers or colors in python. This is a built-in module in python. It is useful to select things randomly, and it is also useful to shuffle the things in the list.

## Generating Random Color in Python Using random() Function in RGB Format

Code

```import random
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)
```

Explanation

First, importing a random function to get the random color in python. A variables r is for red color, g is for green, and b is for blue color. We know that the RGB format has an integer value from 0 to 255. So we are giving the range as 0 to 255. It will take any value from the range. random.randint() is a method to give the range.

Output

`A Random color is : [172, 68, 77]`

## Generating Random Color in Python Using Random Function in Hexadecimal Format

Code

```import random
hexadecimal = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
```

Explanation

First, importing a random module to generate the random color in hexadecimal format. And then using the join() function to join the # and color code. Color code will always start with #. Using for loop to iterate. Now the color code is generated.

Output

`A Random color is : ['#1E8D5C']`

## Generating Random Color Using NumPy Library

Code

```import numpy as np
random_color=list(np.random.choice(range(255),size=3))
print("A Random color is:",random_color)
```

Explanation

First importing Numpy library as np. Next assigning a value and size of the color in a variable random_color. The color will display in the list because we declared it as a list—next, printing random_color.

Output

`A Random color is: [64, 217, 111]`

## Generating Random color Using Matplotlib Library

Code

```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()
```

Explanation

First importing matplotlib library. Next, importing a random module. Next, assign a value in a variable named no_of_colors. And then using the join() function to join the # and color code. Color code will always start with #. Using for loop to iterate. Now the color code is generated. The color will display in the list because we declared it as a list—next, printing random_color.

Output

`['#4EC62C', '#DD382B', '#EB700E', '#4100C8', '#DBDA94']`

## How to Generate a List of 50 Random Colors in Python?

Code

```import random
for j in range(50):
rand_colors = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
print(rand_colors)
```

Explanation

First, importing a random module to get the random colors. Next to creating a for loop to iterate 50 times to get 50 different colors. And then using the join() function to join the # and color code. Color code will always start with #. Using for loop to iterate. Now the color codes are generated.

Output

```['#B74E09']
['#61B22E']
['#4B2DF7']
['#5EB999']
['#5DBDE7']
['#DD629B']
['#B2A6A3']
['#C9212C']
['#E63DC4']
['#A13C50']
['#4E4327']
['#76A9CA']
['#DD7C03']
['#80D077']
['#48A6B8']
['#AC9FA1']
['#D84CE4']
['#D6FE6E']
['#D67956']
['#158AA4']
['#A7D400']
['#381902']
['#0FBA51']
['#EF274A']
['#9E6D0A']
['#578C79']
['#990B61']
['#E4BB06']
['#FB8136']
['#1DEEF5']
['#BB34C2']
['#89FEAC']
['#49577E']
['#E9BB2A']
['#4EFF67']
['#A262DF']
['#8BB529']
['#3D714E']
['#DAEB9D']
['#A1B35B']
['#31F277']
['#A43B68']
['#1E7DB8']
['#81BBA2']
['#6B1CAA']
['#24F932']
['#757B02']
['#7415F6']```

## Making Random Colors in Python Using Turtle Module

Code

```from turtle import *
from random import randint
speed(0)
pensize(5)
colormode(255)
while True:
color(randint(0, 255),
randint(0, 255),
randint(0, 255))
begin_fill()
circle(10)
end_fill()
penup()
goto(randint(-300, 300), randint(-200, 170))
pendown()
```

From turtle library importing star. From random module importing randint. Declaring pensize, speed, and colormode(). Colormode is 255 so that it will show all the colors. Color will be displayed till the condition becomes false. Using begin_fill to start filling the color. The size of the circle is 10. using end_fill to end filling the color. Using penup() to start drawing and pendown() to stop drawing.

Output

## Generating Random Color Palette Using Seaborn

Code

```import seaborn as sns
palette = sns.color_palette(None, 3)
print(palette)
```

First importing seaborn library. A variable palette is holding the color_palette. Next, printing the palette.

Output

`[(0.12156862745098039, 0.4666666666666667, 0.7058823529411765), (1.0, 0.4980392156862745, 0.054901960784313725), (0.17254901960784313, 0.6274509803921569, 0.17254901960784313)]`

1. Which module is used to generate random colors in Python?

The random() module is used to generate random colors in python.

2. What are the two formats to generate the color?

RGB format and hexadecimal format are the formats to generate the color.

3. What are the libraries useful to generate colors randomly?

Numpy, Matplotlib, and turtle are the libraries useful to generate colors randomly.

## Conclusion

Here we have seen about generating random colors in python. We have used a lot of methods and formats to generate colors. These are the methods that are available to generate random colors.

Subscribe
Notify of 