A comprehensive collection of random walk simulations in 1D, 2D, and 3D with interactive visualizations, animations, and statistical analysis.
| 1D Random Walk | 2D Random Walk | 3D Random Walk |
|---|---|---|
![]() |
![]() |
![]() |
- π Introduction
- β¨ Features
- π Folder Structure
- π οΈ Setup and Installation
- π Usage
β οΈ Important Notes- π Notebook Content Overview
- ποΈ Animation & Visual Assets
- π Statistics and Analysis
- π§© Interactive Features
- π¬ Advanced Analysis
- π References
- π€ Contributing
- π License
A random walk is a mathematical object, known as a stochastic or random process, that describes a path that consists of a succession of random steps on some mathematical space such as the integers. This notebook provides a hands-on approach to understanding random walks by:
- Simulating paths in 1D, 2D, and 3D.
- Analyzing statistical properties like mean displacement and standard deviation.
- Visualizing trajectories and distributions using
matplotlibandplotly. - Creating interactive animations to observe the walk's evolution over time.
- Exploring the effects of bias on the walker's path.
- Multi-Dimensional Simulations: Explore random walks in 1D, 2D, and 3D space.
- Statistical Analysis: Calculate and visualize mean position, standard deviation, and final position distributions over multiple trials.
- Biased Walks: Simulate and analyze random walks where steps in certain directions are more probable.
- Interactive Visualizations: Utilize
plotlyandipywidgetsfor dynamic plots. - Animations: Generate
.mp4or.gifanimations of random walks usingmatplotlib.animation. - Quadrant Analysis (2D): Track the walker's time in each quadrant.
- Step Size Comparison (2D): Analyze the effect of different step sizes.
- Simultaneous Walks (3D): Visualize multiple walkers starting from different positions.
.
βββ ποΈ ANIMATION/
β βββ ποΈ 1D/ # Animations for 1D walks (.mp4, .gif)
β βββ ποΈ 2D/ # Animations for 2D walks (.mp4, .gif)
β βββ ποΈ 3D/ # Animations for 3D walks (.mp4, .gif)
βββ πΌοΈ IMAGES/
β βββ πΌοΈ 1D/ # Static plots for 1D walks (.png, .jpg)
β βββ πΌοΈ 2D/ # Static plots for 2D walks (.png, .jpg)
β βββ πΌοΈ 3D/ # Static plots for 3D walks (.png, .jpg)
βββ π STATISTICS/ # Optional: CSV or other files with statistical results
βββ π§ͺ RandomWalk.ipynb # The main Jupyter Notebook file
βββ π¦ requirements.txt # List of required Python packages
βββ π README.md # This file
You can follow these steps to set up your environment and run the notebook.
- Python 3.7 or higher
pip(Python package installer)git(for cloning the repository)
git clone https://github.com/PuspenduPH/Random-Walk-Simulations
cd Random-Walk-SimulationsUsing a virtual environment prevents dependency conflicts.
- Create a virtual environment (e.g., named
randomwalk_env):python -m venv randomwalk_env
- Activate the environment:
- Windows:
randomwalk_env\Scripts\activate
- Linux/macOS:
source randomwalk_env/bin/activate
- Windows:
- (Optional) Upgrade pip:
python -m pip install --upgrade pip
Install all required Python libraries using the requirements.txt file:
pip install -r requirements.txtAlternatively, install them manually:
pip install numpy scipy scikit-learn matplotlib plotly ipywidgets pandas tqdm ipymplNote: FFmpeg is necessary to save animations in .mp4 format and must be installed manually (see Important Notes below).
- Ensure your virtual environment is activated.
- Navigate to the repository directory in your terminal.
- Launch Jupyter Notebook or JupyterLab:
jupyter notebook # OR jupyter lab - Open the
random_walk_notebook.ipynb(or your actual notebook file name) in the Jupyter interface. - Run the cells sequentially or explore specific sections.
This notebook contains interactive visualizations, animations, and data manipulation tools. Follow these instructions to avoid common errors and ensure smooth execution:
-
Animation in Jupyter:
- Install the
ipymplpackage with:pip install ipympl
- Use the following magic commands at the top of your notebook:
%matplotlib ipympl # For displaying animations %matplotlib widget # For interactive widgets
- Tip: Use only one magic command in a cell, depending on your requirements. If you need any help installing
ipymplyo can follow this link
- Install the
-
Saving Animations as .mp4:
- Install FFmpeg:
- Windows:
- Download FFmpeg from ffmpeg.org.
- Extract and add the
binfolder to your system's PATH.
- macOS/Linux:
brew install ffmpeg # For macOS with Homebrew sudo apt install ffmpeg # For Ubuntu/Debian
- Windows:
- Install FFmpeg:
-
General Tips:
- Use
FuncAnimationfrommatplotlib.animationfor creating animations. - Utilize
PillowWriterif you prefer saving animations as.gif. - Use
tqdmfor progress tracking in loops or simulations. - Suppress unnecessary warnings:
import warnings warnings.filterwarnings("ignore")
- Use Pythonβs
tracebackmodule for inline error logging during debugging.
- Use
Reminder: Always restart the kernel after switching
%matplotlibbackends or installing new Jupyter-related packages to avoid rendering issues.
The notebook is divided into sections exploring random walks in increasing dimensions:
-
Simulation: Basic simulation of a single 1D random walk.
-
Analysis: Calculating displacement and distance from the origin.
-
Multiple Trials: Running simulations multiple times to observe average behaviour.
-
Biased Random Walk: Introducing probabilities for moving left or right.
| Probability p=0.7 | Probability p=0.3 |
|---|---|
![]() |
![]() |
-
Mean and Standard Deviation Analysis: Examining how mean position and standard deviation evolve.
-
Distribution of Walker's Position: Visualizing the probability distribution of the walker's position after N steps.
-
Animation: Animating the 1D random walk.
-
Animation with Position Distribution: Animating the walk alongside its evolving position distribution.
rdwalk_1D_dist_s50_t40_p0.5.mp4
-
Simulation & Plotting: Simulating and plotting a 2D walk using
matplotlibandplotly.
-
Contour Plot: Visualizing the density of visited locations.
-
-
Quadrant Analysis: Tracking the proportion of time spent in each quadrant.
-
Quadrant Analysis with Animation: Animating the walk and the evolving quadrant distribution.
-
Analysis with Contour Plot and Heatmap: Combined visualisation of path and visit frequency.
-
Distribution of Walker's Position: Analyzing the 2D distribution of positions over multiple trials.
-
Distribution of Final Position: Visualising where walkers end up after N steps.
-
Animation: Basic animation of the 2D random walk path.
-
Animation with Position Frequency: Animating the walk alongside a heatmap of visited positions.
-
Step Size Comparison: Analysing walks with different step lengths.
-
2D Biased Random Walk: Implementing bias in movement directions (e.g., tendency to move North-East).
2D Random Walk with Angular Bias 2D Random Walk with Target Bias 

-
Comparison with/without Bias: Visual comparison of biased vs. unbiased walks.
-
Biased Walk with Different Probabilities: Exploring various bias configurations.
rdwalk_2D_with_counts_s100_t20_p0.4_q0.1_r0.25_s0.25.mp4
-
Simulation (Single Trial): Simulating and visualizing a single walk in 3D space using
matplotliborplotly.random_walk_3d_s100.mp4
-
Simulation (Multiple Trials): Running and analyzing multiple 3D walks.
random_walk_3d_multi_s50_t20_1.1.mp4
-
Multiple Trials with Different Initial Positions: Simulating walkers starting from various points.
random_ip_walk_3d_multi_s50_t20.mp4
-
Distribution of Walker's Final Position: Visualizing the 3D distribution of endpoints.
-
Simultaneous Random Walk: Animating multiple walks concurrently.
-
Biased Random Walk: Introducing directional bias in 3D steps.
-
3D Random Walk with Target Bias
distance_biased_rdwalk_3d_s50_t15.mp4
-
3D Random Walk with Angular Bias
angular_biased_rdwalk_3d_s50_t15.mp4
-
-
Visualise Biased Walks with IpyWidgets: Using widgets to explore different bias parameters interactively.
-
Comparison with/without Bias: Comparing trajectories of biased and unbiased 3D walks.
final_walk_comparison_3d_s50_t15.mp4
Animations and visual outputs are organized into separate directories by dimensionality:
- ANIMATION/1D/ β Animations for 1D random walk.
- ANIMATION/2D/ β Animations for 2D random walk.
- ANIMATION/3D/ β Animations for 3D random walk.
- IMAGES/1D/, IMAGES/2D/, IMAGES/3D/ β Supporting images and figures.
Please add, update, or replace assets as your experiments change.
The STATISTICS folder is intended for:
- Storing detailed statistical reports and datasets related to random walk experiments.
- Tracking parameters such as mean, variance, skewness, and other performance metrics of the simulations.
This modular organization makes maintaining a historical record of simulation outcomes and analysis easier.
The notebook includes several interactive components:
- Adjustable parameters for walk length, bias, and step size
- Real-time visualization updates
- Animated simulations with playback controls
- Parameter exploration with ipywidgets
- Statistical analysis with immediate feedback
Statistical investigations included in the notebook:
- Mean square displacement analysis
- First return times
- Probability distributions
- Central limit theorem demonstrations
- Quadrant occupation statistics
- Bias effects on displacement
- Matplotlib Animation Guide
- Plotly Documentation
- Jupyter ipympl Documentation
- FFmpeg Installation
- Wikipedia: Random Walk
- Lawler, G. F., & Limic, V. (2010). Random walk: a modern introduction (Vol. 123). Cambridge University Press.
- MΓΆrters, P., & Peres, Y. (2010). Brownian motion (Vol. 30). Cambridge University Press.
- Spitzer, F. (2013). Principles of random walk (Vol. 34). Springer Science & Business Media.
- Bhattacharyya, Arka et al. βA Brief Study on Simple Random Walk in 1D.β Resonance 28 (2023): 945 - 958.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository.
- Create your feature branch (
git checkout -b amazing-feature). - Commit your changes (
git commit -m 'Add some amazing feature'). - Push to the branch (
git push origin amazing-feature). - Open a Pull Request.
Distributed under the MIT License. See LICENSE for more information.




