A Monte Carlo simulation of particles undergoing random walks in a bounded 2D domain with elastic wall reflections.
Each particle moves at a fixed step length in a randomly assigned direction. The direction angle is drawn uniformly from [0, 2*pi). This discrete-time random walk approximates Brownian motion in the continuum limit: the displacement of each particle in each coordinate follows an approximately normal distribution over many steps (Central Limit Theorem).
When a particle's position (plus a margin for its radius) exceeds the box limits [-L, L] x [-L, L], the angle is reflected:
- Horizontal walls: angle -> -angle
- Vertical walls: angle -> pi - angle
This implements elastic reflection and confines the random walk to a bounded domain.
When particles all start from the origin (concentration = 0), we observe the diffusion process: the particle cloud expands outward. Over time, with wall reflections, the spatial distribution approaches a uniform equilibrium across the box -- a consequence of the ergodic theorem for reflected random walks.
Starting from the origin, particles diffuse outward and eventually fill the box:
Distribution of particle positions (x, y), distance from origin, and mean displacement over time:
Particles starting from random positions:
Particles starting from the same point -- watch the diffusion process:
Step-by-step view of 1000 particles at the first wall reflection:
- Diffusion rate: The mean distance from the origin grows proportionally to sqrt(t) in the early phase (before wall interactions dominate), consistent with the classical diffusion relation.
- Equilibrium: After sufficiently many steps, the spatial distribution becomes approximately uniform within the box, independent of starting configuration.
- Geometric patterns at boundaries: When the expanding particle cloud first hits a wall, interesting geometric patterns emerge from the simultaneous reflection of many particles.
balls moving in a box.ipynb-- Main simulation notebookColormap reference.ipynb-- Matplotlib colormap reference used for particle visualization

















