Log In  


Cart #kakiwihzu-1 | 2025-08-12 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

I just had to make this!

This is my version of a Galton Board. This toy demonstrates the Central Limit Theorem (CLT). As I understand it, the CLT is simply the tendency for a series of random events to converge into a bell curve! It is a useful concept in statistics that is borrowed from probability. It is also relevant in AI and machine learning, where bell curves can be used to model deviations (from an average), error, or noise

Controls are listed in the cartridge

If you are interested in the math side of things, there are basically three layers:

  1. Every time a particle hits one of the fixtures, a coin flip occurs, sending the particle left or right randomly. This happens up to 9 times, depending on where the particle started

  2. Once a particle reaches the bottom row of fixtures, a similar coin flip occurs, except the particle can be sent up to 6 pixels away. This was done to more evenly distribute the particles as they fall to the bottom

  3. When a particle lands on the pile below, it has a chance to move left or right, if there is space. This further smooths out the pile, and makes the whole simulation more sand-like

I hope you enjoy!


Dev Journey Side Note:

This was mostly enjoyable to code. I thought I needed a flashy solution to render all of the particles, but using just pset() turned out fine. If more realistic physics were in play, then I'm sure some tricks would be necessary to make all the particles act "real"

Coming up with the layers was quite a challenge. I thought this would be easy, only requiring the initial coin flips. Sadly, this does not produce a smooth pile of sand as one might think. It took many iterations to get to the final result - yet another example where good design always goes through some iteration

I had one big bug which maybe some of you would enjoy hearing about. When it came to adjusting the speed, it seemed as simple as modifying the code that affects each particle's y coordinate. This was all well and good, until a bug came up

Sometimes, particles would seem to just pass right through the stack they were supposed to land on. It didn't happen until I allowed coordinates to take on decimal values, and it didn't happen to every particle. No amount of flr()'ing or even ceil()'ing (ha) seemed to fix it. So about an hour or two later (and with next to no help from AI - not knocking it too hard), I figured out what went wrong...

I must have stop()'d the program and walked through frame by frame to figure out what exactly was happening. To be brief, sometimes a particle would land on a stack, increasing it by one, yet another particle was right behind it. Since particles could move a distance of .5 at a time, the stack could increase by one and step right over the particle, letting it fall through. Pretty icky. If you slowed down the program, you would see particles occasionally overlap, since they had the same floor. I quickly figured out that all I needed was to space out the particles relative to their speed. That way no particle could "sneak" behind the other

Watch out for decimals!

2



[Please log in to post a comment]