sinθ sinθ - 26 days ago 12
Python Question

Generating a Random Hex Color in Python

For a Django App, each "member" is assigned a color to help identify them. Their color is stored in the database and then printed/copied into the HTML when it is needed. The only issue is that I am unsure how to generate random

Hex
colors in python/django. It's easy enough to generate RGB colors, but to store them I would either need to a) make three extra columns in my "Member" model or b) store them all in the same column and use commas to separate them, then, later, parse the colors for the HTML. Neither of these are very appealing, so, again, I'm wondering how to generate random
Hex
colors in python/django.

Answer
import random
r = lambda: random.randint(0,255)
print('#%02X%02X%02X' % (r(),r(),r()))