Recently I was going through some old code that I had lying around, and found this little piece. I had been procrastinating on wikipedia one day and found the article on Chaos Games. At the bottom there's a link to a page with an applet to generate the fractals. Of course I had to have a go, and managed to whip together this:

```
from PIL import Image, ImageDraw
import random
size = (300,300)
im = Image.new('RGB', size)
draw = ImageDraw.Draw(im)
x, y = random.random(), random.random()
for i in xrange(50000):
rand = random.random()
if rand < 0.01:
x, y = 0.0, 0.16 * y;
elif rand < 0.86:
newx = (0.85 * x) + (0.04 * y)
newy = (-0.04 * x) + (0.85 * y) + 1.6
x, y = newx, newy
elif rand < 0.93:
newx = (0.2 * x) - (0.26 * y)
newy = (0.23 * x) + (0.22 * y) + 1.6
x, y = newx, newy
else:
newx = (-0.15 * x) + (0.28 * y)
newy = (0.26 * x) + (0.24 * y) + 0.44
x, y = newx, newy
draw.point(
(size[0]/2.0 + x*size[0]/10.0, y*size[1]/12.0),
fill='#0f0')
im.save("fern.png", "PNG")
```

I have no idea how it works mathematically, but it's quite satisfying to make something so complex with such a small amount of code.

To get the same image that I got, you'll have to play around with the image size and the size of the sample — I generated a much larger image with a huge sample and shrunk it for the web — there seems to be a sweet spot for the number of iterations required for an image size.