Sierpiński triangle

2015-07-18, post № 47

mathematics, programming, Pygame, Python, #3, #3-gon, #around the center, #black, #black and white, #fractal, #generates, #generating, #generator, #three, #triangle, #white

Having recently heard something about chaos theory, I was interested in the Sierpiński triangle. Specifically in the random generation. To prove that it can really be generated using a die, I wrote this program.

How it works

  • You start with any point inside the triangle (I chose the center).
  • A triangle has three points, let us call them 𝐴, 𝐵 and 𝐶.
  • You choose randomly between one of those three points, go half the distance and set a pixel right there.
  • Repeating this process over and over again (always starting at your last point), you eventually [1] get the Sierpiński triangle.


  • ‘Space’ toggles if the triangle is generated,
  • ‘m’ toggles if the current position is shown (indicated by a blue circle).
Source code: sierpinski-triangle.py


  1. [2020-07-08] This post in particular was slightly edited to better reflect current standards. A note one may add is that “eventually” should really say “in the limit”, as no finite amount of time will lead to the true fractal using the described methodology.