Randomness in art: make every piece one of a kind
Perfectly matching shapes can look a little boring. Sprinkle in some randomness — a surprise size here, a surprise color there — and your art comes alive.
The big idea
Random numbers let each shape have its own size and color, so no two look the same.
See it in code
Randomness means a surprise. random.choice(PALETTE) grabs a surprise color from our set. Here it colors one big star. We seed it so you see this pick:
import random
from art import Canvas, PALETTE, star
screen = Canvas.create()
Canvas.fill(screen)
random.seed(5)
color = random.choice(PALETTE)
star(screen, 250, 250, size=120, color=color)
You didn't pick the color — the code did. Change the seed and the star changes color.
Now a short row. Three stars, and each grabs its own random color. Same size, same spacing — only the color is a surprise:
import random
from art import Canvas, PALETTE, star
screen = Canvas.create()
Canvas.fill(screen)
random.seed(2)
for i in range(3):
color = random.choice(PALETTE)
star(screen, 110 + i * 140, 250, size=80, color=color)
Three stars, three surprises. The row stays neat, but the colors keep it lively.
Now surprise the size too. We draw nine stars in a row. random.randint(25, 70) picks a different size for each, and random.choice(PALETTE) picks a different color. Same row, all unique:
import random
from art import Canvas, PALETTE, star
screen = Canvas.create()
Canvas.fill(screen)
random.seed(8)
for i in range(9):
size = random.randint(25, 70)
color = random.choice(PALETTE)
star(screen, 60 + i * 48, 250, size=size, color=color)
The stars line up in a neat row. But each one has its own size and color. Randomness gave you the variety — you didn't pick nine sizes and colors by hand.
A little randomness makes things feel real. Think of leaves on a tree, or stars in a sky. Real things aren't all the same. A bit of random variety makes your art look real, not stamped out.
Try it yourself
Add random y too, with y = random.randint(150, 350), so the stars bob up and down. Then narrow the size range to random.randint(40, 45) and see how much calmer it looks.
The common mistake
Randomizing so much there's no pattern left. Keep something steady — here the stars share a row and even spacing. A little randomness on top of order is what looks great.
What it unlocks
Random variety builds on the random module and RGB color, and is the heart of generative art.