create random RGB color code using python

Today we are going to learn how to generate RGB color code using python. Nowadays the hex color code is very useful in any designing part. In CSS part color is used as RGB and Hex code format. In python, it is very easy to create/generate any random rgb value.

Let’s go for the coding section:

Random RGB Color value generate in python

Requirement:

  • random library

Related articles on random module :

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 RGB Value :',rgb)

Output:

A Random RGB Value : [236, 58, 44]

Process:

In this code at first, we import the random library to work.

Then we generate three random integer number by using random.randint() function, which must be in between lower value 0 and upper value  255. And store the three value in ‘r’, ‘g’, ‘b’ variables.

Then we create a ‘rgb’ list variable and put the r,g,b into the ‘rgb’ list.

Finally, we get a random RGB code and we print the value of the ‘rgb’ variable.

Note:

random.randint()

This method is can take up to two parameters.

random.randint(start_range,end_range)

And you must be careful about the datatype. As you can see this is randint and this method only takes integer type parameter(s).

Leave a Reply

Your email address will not be published. Required fields are marked *