# 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.
# Create a random 8‑bit integer (0–255).
# rand(256) returns a number in [0, 256),
# giving exactly one byte of color data.
def random_channel
# 8 bits → values from 0 to 255
rand(256)
end
# Produce a random RGB color by combining the channels.
def generate_random_rgb
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 = "rgb(#{r}, #{g}, #{b})"
{ r: r, g: g, b: b, rgb: rgb }
end
# Run the program
result = generate_random_rgb
puts "Red (8 bits): #{result[:r]}"
puts "Green (8 bits): #{result[:g]}"
puts "Blue (8 bits): #{result[:b]}"
puts "RGB color: #{result[:rgb]}"
=begin
run:
Red (8 bits): 231
Green (8 bits): 4
Blue (8 bits): 204
RGB color: rgb(231, 4, 204)
=end