Python

Generate Random RGB Colors in Python

To generate RGB colors, the user must first understand the most common representation of RGB. These Color Values are either represented in a 6-digit Hexadecimal Format or a list of 3 different values ranging from 0 to 255. To do this, the user can utilize the choice() method to generate a hexadecimal representation of a random RGB color or the randint() method to generate a list of 3 separate values.

The content of this post includes:

Let’s start with the first method.

Method 1: Using the choice() Method to Generate RGB Hex Code

The hex code representation of an RGB color starts with a hash symbol “#” followed by 6 hexadecimal characters. The hexadecimal characters include numbers from 0-9 and Alphabets A to F. To generate a random hexadecimal RGB value, use the following code snippet:

import random
rgbValue=""
for i in range(6):
    rgbValue += random.choice("0123456789ABCDEF")

rgbValue = "#"+rgbValue

print(rgbValue)

 
In this above code snippet:

    • The “random” package is imported to utilize the choice() method.
    • An empty string “rgbValue” is declared.
    • The choice() method is executed to choose a hexadecimal character 6 times with a for loop and these 6 characters are combined using the string concatenation operator.
    • Lastly, the prefix “#” is added using the string concatenation as well.

When this code is executed, it displays the following result:


As you can see an RGB color value has been generated. However, it would be a better practice to convert this code into a function and call that function to generate multiple colors. Take the following code for a function-based approach:

import random
def randRGB():
    rgbValue=""
    for i in range(6):
        rgbValue += random.choice("0123456789ABCDEF")

    rgbValue = "#"+rgbValue
    return rgbValue

for i in range(10):
    print(randRGB())

 
When this code is executed, it produces the following results:


In the output, you can clearly observe that the code has generated 10 random RGB values in Hexadecimal format.

Method 2: Using the randint() Method to Generate RGB Values

The randint() method of the “random” package can be used to generate integer values, which is very useful when you want to represent the RGB values according to their individual intensity. The RGB values are represented as a set of 3 values, each value represents the intensity of the color from 0 to 255. The first value is Red, then Green, and the last Blue. To generate these sets of intensity values for RGB colors, use the following code:

import random

randomRGB = []
for i in range(3):
    randomRGB.append(random.randint(0,255))
print(randomRGB)

 
In this code snippet:

    • The “random” package is import to utilize its randint() method.
    • An empty list “randomRGB” which will be used to hold the three values of Red, Green, and Blue.
    • The randint() method is used to choose a value between 0 and 255 three times with the help of a for loop.
    • These values are appended in the list using the append() method.
    • Lastly, the list randomRGB is printed onto the terminal.

When this code is executed, it will produce the following results:


The output shows that a random RGB value was generated. However, just like the first method, you can use a function-based approach for better modularization:

import random

def getRandRGB():
    randomRGB = []
    for i in range(3):
        randomRGB.append(random.randint(0,255))
    return(randomRGB)

for i in range(10):
    print(getRandRGB())

 
Running this code will produce the following result on the terminal:


As you can see from the output that 10 different random RGB values were generated and printed onto the terminal.

Conclusion

To generate random RGB values in Python, the user can utilize the choice() method and the randint() method from the “random” package/module. The choice() works best if the goal of the user is to generate the hexadecimal code of the RGB color. On the other hand, the randint() method works best if the user wants to generate separate intensity values for the three colors which are Red, Green, and Blue. This post has shown the two best possible ways to generate random RGB Colors in Python.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.