A C++ simulation of a Galton Board, with support for automated experiments and analysis via a Python notebook.
Clone the repository:
git clone https://github.com/PauJimeno/RA_Assignment-1.git
cd RA_Assignment-1Compile the C++ code using g++:
g++ -o galton_board galtonBoard.cppNo external libraries have been used.
The simulation supports several parameters. Use --help or -h to view usage:
./galton_board --helpExample output:
| FLAG | TYPE | DESCRIPTION | DEFAULT |
|---|---|---|---|
| -d / --min | <int> |
Minimum number of balls/layers | 1000 |
| -u / --max | <int> |
Maximum number of balls/layers | 10000 |
| -s / --step | <double> |
Step value (linear or multiplier in log mode) | 1000 |
| -n / --nfixed | <int> |
value of fixed vaiable | 100 |
| -p / --prob | <double> |
Probability of right side | 0.5 |
| -v / --vlayers | Set balls to be the fixed variable | false | |
| -l / --logstep | Use logarithmic steps | false |
Notes:
- You can fix one variable (balls or layers) and vary the other.
- The free variable progresses from
--minto--max. - Step size is controlled by
--step. Use--logstepfor logarithmic progression.
- Fixed layers (300) with increasing balls (5–10000) in linear steps of 100:
./galton_board --nfixed 300 --min 5 --max 10000 --step 100- Fixed balls (300) with increasing layers (5–10000) in linear steps of 100:
./galton_board --nfixed 300 --min 5 --max 10000 --step 100 --vlayersThe simulation prints results in CSV format, where each column represents a final bin and the value is the number of balls in that bin.
Example (55 balls, 15 layers):
0;0;0;0;1;4;10;6;17;8;5;3;1;0;0;0
A Python notebook (GaltonBoard_graphs.ipynb) is provided to run simulations, analyze results, compute errors, and generate plots.
Requires numpy, scipy, matplotlib.
exec_name = "galton_board" # Name of the compiled executable
n = 100 # Fixed quantity (layers or balls)
min_value = 5 # Minimum for variation
max_value = 10000 # Maximum for variation
step = 1.3 # Step value (>1)
log = True # Use logarithmic steps
vl = False # If True, balls are fixed; otherwise, layers are fixed
p = 0.5 # Probability to go left
n_s = 25 # Number of samples
main(exec_name, n, min_value, max_value, step, vl, log, p, n_it)The main function:
- Runs the C++ simulation in a subprocess.
- Collects results.
- Generates plots for analysis.
More details can be found in the notebook.
The notebook produces plots such as:
Pau Jimeno Román Thomas Aubertier