I wrote this algorithmically generated lightning while working on a piece for the new Energy Gallery in the London Science Museum.
I've since refined it a little and thought I'd post it up here for you to play with.
I tried to make it work as close to my best guess of how real lightning works as possible.
As you click to emit the lightning a random charge is placed at the point of your click.
While the charge deposited is sufficiently large lightning forks are produced that make the jump
to the ground and take with them an amout of the initial charge.
The fork forges a path of least resistance toward the ground. The deviation of the fork at each step accumulating as it travels groundwards.
How long a fork lasts is determined by the size of it's share of the initial charge and the thickness of it's beam. The greater a piece of the initial charge it takes the longer the fork will last but the thicker the fork beam the quicker it's charge will dissapate and so shorten it's lifetime.
If the path of the fork follows a sufficiently sharp kink a child branch is born and a portion of the fork's charge escapes along it's
own path of least resistance towards the ground.
Child branches reduce the lifetime of their parent fork as they increase the speed with which the fork's charge is transmited to the ground.
Finally, if a sufficient amout of the initial charge remians when the existing fork or forks run out of energy, a further fork is born to continue the transferal of the charge.
This process is repeated untill there is no longer sufficient charge left to make the jump from the point of emition to the ground.» launch