This is a Python script that generates a grid of random HTML colors. It works by reading random data from /dev/urandom
and then encoding it as a hexadecimal string. This string is spit into individual values that will be represented as a color.
I am wanting to know if this is the best way to generate random values for this task. Would it be better to use the random
module? Or is my method more beneficial for this task?
WIDTH = 64
HIGHT = 5
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
data = file("/dev/urandom").read(HIGHT*WIDTH*3).encode("hex")
colors = ""
count = 0
for byte in chunks(data, 6):
if count == WIDTH:
colors+="<br>"
count=0
count+=1
colors+="""<p style="color:%s; display:inline;">■</p>""" % (byte)
file("colors.html", "w").write(colors)
This is the output: