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
- random library
Related articles on random module :
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)
A Random RGB Value : [236, 58, 44]
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.
This method is can take up to two parameters.
And you must be careful about the datatype. As you can see this is randint and this method only takes integer type parameter(s).