| 1 | #!/bin/bash |
| 2 | # brownian.sh |
| 3 | # Author: Mendel Cooper |
| 4 | # Reldate: 10/26/07 |
| 5 | # License: GPL3 |
| 6 | |
| 7 | # ---------------------------------------------------------------- |
| 8 | # This script models Brownian motion: |
| 9 | #+ the random wanderings of tiny particles in a fluid, |
| 10 | #+ as they are buffeted by random currents and collisions. |
| 11 | #+ This is colloquially known as the "Drunkard's Walk." |
| 12 | |
| 13 | # It can also be considered as a stripped-down simulation of a |
| 14 | #+ Galton Board, a slanted board with a pattern of pegs, |
| 15 | #+ down which rolls a succession of marbles, one at a time. |
| 16 | #+ At the bottom is a row of slots or catch basins in which |
| 17 | #+ the marbles come to rest at the end of their journey. |
| 18 | # Think of it as a kind of bare-bones Pachinko game. |
| 19 | # As you see by running the script, |
| 20 | #+ most of the marbles cluster around the center slot. |
| 21 | #+ This is consistent with the expected binomial distribution. |
| 22 | # As a Galton Board simulation, the script |
| 23 | #+ disregards such parameters as |
| 24 | #+ board tilt-angle, rolling friction of the marbles, |
| 25 | #+ angles of impact, and elasticity of the pegs. |
| 26 | # To what extent does this affect the accuracy of the simulation? |
| 27 | # ---------------------------------------------------------------- |
| 28 | |
| 29 | PASSES=500 # Number of particle interactions / marbles. |
| 30 | ROWS=10 # Number of "collisions" (or horiz. peg rows). |
| 31 | RANGE=3 # 0 - 2 output range from $RANDOM. |
| 32 | POS=0 # Left/right position. |
| 33 | RANDOM=$$ # Seeds the random number generator from PID |
| 34 | #+ of script. |
| 35 | |
| 36 | declare -a Slots # Array holding cumulative results of passes. |
| 37 | NUMSLOTS=21 # Number of slots at bottom of board. |
| 38 | |
| 39 | |
| 40 | Initialize_Slots () { # Zero out all elements of the array. |
| 41 | for i in $( seq $NUMSLOTS ) |
| 42 | do |
| 43 | Slots[$i]=0 |
| 44 | done |
| 45 | |
| 46 | echo # Blank line at beginning of run. |
| 47 | } |
| 48 | |
| 49 | |
| 50 | Show_Slots () { |
| 51 | echo; echo |
| 52 | echo -n " " |
| 53 | for i in $( seq $NUMSLOTS ) # Pretty-print array elements. |
| 54 | do |
| 55 | printf "%3d" ${Slots[$i]} # Allot three spaces per result. |
| 56 | done |
| 57 | |
| 58 | echo # Row of slots: |
| 59 | echo " |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|" |
| 60 | echo " ||" |
| 61 | echo # Note that if the count within any particular slot exceeds 99, |
| 62 | #+ it messes up the display. |
| 63 | # Running only(!) 500 passes usually avoids this. |
| 64 | } |
| 65 | |
| 66 | |
| 67 | Move () { # Move one unit right / left, or stay put. |
| 68 | Move=$RANDOM # How random is $RANDOM? Well, let's see ... |
| 69 | let "Move %= RANGE" # Normalize into range of 0 - 2. |
| 70 | case "$Move" in |
| 71 | 0 ) ;; # Do nothing, i.e., stay in place. |
| 72 | 1 ) ((POS--));; # Left. |
| 73 | 2 ) ((POS++));; # Right. |
| 74 | * ) echo -n "Error ";; # Anomaly! (Should never occur.) |
| 75 | esac |
| 76 | } |
| 77 | |
| 78 | |
| 79 | Play () { # Single pass (inner loop). |
| 80 | i=0 |
| 81 | while [ "$i" -lt "$ROWS" ] # One event per row. |
| 82 | do |
| 83 | Move |
| 84 | ((i++)); |
| 85 | done |
| 86 | |
| 87 | SHIFT=11 # Why 11, and not 10? |
| 88 | let "POS += $SHIFT" # Shift "zero position" to center. |
| 89 | (( Slots[$POS]++ )) # DEBUG: echo $POS |
| 90 | |
| 91 | # echo -n "$POS " |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | Run () { # Outer loop. |
| 97 | p=0 |
| 98 | while [ "$p" -lt "$PASSES" ] |
| 99 | do |
| 100 | Play |
| 101 | (( p++ )) |
| 102 | POS=0 # Reset to zero. Why? |
| 103 | done |
| 104 | } |
| 105 | |
| 106 | |
| 107 | # -------------- |
| 108 | # main () |
| 109 | Initialize_Slots |
| 110 | Run |
| 111 | Show_Slots |
| 112 | # -------------- |
| 113 | |
| 114 | exit $? |
| 115 | |
| 116 | # Exercises: |
| 117 | # --------- |
| 118 | # 1) Show the results in a vertical bar graph, or as an alternative, |
| 119 | #+ a scattergram. |
| 120 | # 2) Alter the script to use /dev/urandom instead of $RANDOM. |
| 121 | # Will this make the results more random? |
| 122 | # 3) Provide some sort of "animation" or graphic output |
| 123 | # for each marble played. |