Random Color Generator in Hexadecimal Format

  • Share this:

Code introduction


This function generates a random color represented as a hexadecimal string. The color value consists of three RGB components (red, green, and blue), each being an integer between 0 and 255.


Technology Stack : Python

Code Type : Function

Code Difficulty :


                
                    
import random

def generate_random_color():
    """
    Generate a random color represented as a hexadecimal string.
    """
    def random_int():
        return random.randint(0, 255)
    
    return "#{:02x}{:02x}{:02x}".format(random_int(), random_int(), random_int())                
              
Tags: