Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to generate a random color in RGB format with Python

4 Answers

0 votes
import numpy as np
 
color = list(np.random.choice(range(256), size = 3))
 
print(f"Random RGB Color: rgb({color[0]}, {color[1]}, {color[2]})")
 
 
'''
run:

Random RGB Color: rgb(19, 129, 153)
 
'''
 

 



answered Apr 27, 2021 by avibootz
edited 2 days ago by avibootz
0 votes
import random
 
def generate_random_rgb_color():
    red = random.randint(0, 255)
    green = random.randint(0, 255)
    blue = random.randint(0, 255)
    print(f"Random RGB Color: rgb({red}, {green}, {blue})")
 
 
generate_random_rgb_color()
 
 
'''
run:
 
Random RGB Color: rgb(253, 113, 35)
 
'''

 



answered 2 days ago by avibootz
0 votes
import numpy as np
 
color = [int(c) for c in np.random.choice(range(256), size=3)]

print(f"Random RGB Color: rgb({color[0]}, {color[1]}, {color[2]})")

 
 
'''
run:

Random RGB Color: rgb(68, 9, 174)
 
'''
 

 



answered 2 days ago by avibootz
0 votes
"""
Generate a random color in RGB format: rgb(R, G, B)
This program demonstrates how numbers and bits are used
to produce valid 8‑bit channel values.
"""

import random

def random_channel():
    """
    Create a random 8‑bit integer (0–255).
    random.random() gives a floating‑point number in [0, 1),
    so multiplying by 256 (2**8) gives a range of 8 bits.
    """
    # 8 bits → values from 0 to 255
    return int(random.random() * 256)


def generate_random_rgb():
    """
    Produce a random RGB color by combining the channels.
    """
    r = random_channel()  # Red channel (8 bits)
    g = random_channel()  # Green channel (8 bits)
    b = random_channel()  # Blue channel (8 bits)

    # Construct the CSS-style RGB string
    rgb = f"rgb({r}, {g}, {b})"

    return r, g, b, rgb


# Run the program
r, g, b, rgb = generate_random_rgb()
print("Red (8 bits):", r)
print("Green (8 bits):", g)
print("Blue (8 bits):", b)
print("RGB color:", rgb)


"""
run:

Red (8 bits): 242
Green (8 bits): 120
Blue (8 bits): 74
RGB color: rgb(242, 120, 74)

"""

 



answered 2 days ago by avibootz
...