| 1 | #!/bin/bash |
| 2 | # cannon.sh: Approximating PI by firing cannonballs. |
| 3 | |
| 4 | # Author: Mendel Cooper |
| 5 | # License: Public Domain |
| 6 | # Version 2.2, reldate 13oct08. |
| 7 | |
| 8 | # This is a very simple instance of a "Monte Carlo" simulation: |
| 9 | #+ a mathematical model of a real-life event, |
| 10 | #+ using pseudorandom numbers to emulate random chance. |
| 11 | |
| 12 | # Consider a perfectly square plot of land, 10000 units on a side. |
| 13 | # This land has a perfectly circular lake in its center, |
| 14 | #+ with a diameter of 10000 units. |
| 15 | # The plot is actually mostly water, except for land in the four corners. |
| 16 | # (Think of it as a square with an inscribed circle.) |
| 17 | # |
| 18 | # We will fire iron cannonballs from an old-style cannon |
| 19 | #+ at the square. |
| 20 | # All the shots impact somewhere on the square, |
| 21 | #+ either in the lake or on the dry corners. |
| 22 | # Since the lake takes up most of the area, |
| 23 | #+ most of the shots will SPLASH! into the water. |
| 24 | # Just a few shots will THUD! into solid ground |
| 25 | #+ in the four corners of the square. |
| 26 | # |
| 27 | # If we take enough random, unaimed shots at the square, |
| 28 | #+ Then the ratio of SPLASHES to total shots will approximate |
| 29 | #+ the value of PI/4. |
| 30 | # |
| 31 | # The simplified explanation is that the cannon is actually |
| 32 | #+ shooting only at the upper right-hand quadrant of the square, |
| 33 | #+ i.e., Quadrant I of the Cartesian coordinate plane. |
| 34 | # |
| 35 | # |
| 36 | # Theoretically, the more shots taken, the better the fit. |
| 37 | # However, a shell script, as opposed to a compiled language |
| 38 | #+ with floating-point math built in, requires some compromises. |
| 39 | # This decreases the accuracy of the simulation. |
| 40 | |
| 41 | |
| 42 | DIMENSION=10000 # Length of each side of the plot. |
| 43 | # Also sets ceiling for random integers generated. |
| 44 | |
| 45 | MAXSHOTS=1000 # Fire this many shots. |
| 46 | # 10000 or more would be better, but would take too long. |
| 47 | PMULTIPLIER=4.0 # Scaling factor. |
| 48 | |
| 49 | declare -r M_PI=3.141592654 |
| 50 | # Actual 9-place value of PI, for comparison purposes. |
| 51 | |
| 52 | get_random () |
| 53 | { |
| 54 | SEED=$(head -n 1 /dev/urandom | od -N 1 | awk '{ print $2 }') |
| 55 | RANDOM=$SEED # From "seeding-random.sh" |
| 56 | #+ example script. |
| 57 | let "rnum = $RANDOM % $DIMENSION" # Range less than 10000. |
| 58 | echo $rnum |
| 59 | } |
| 60 | |
| 61 | distance= # Declare global variable. |
| 62 | hypotenuse () # Calculate hypotenuse of a right triangle. |
| 63 | { # From "alt-bc.sh" example. |
| 64 | distance=$(bc -l << EOF |
| 65 | scale = 0 |
| 66 | sqrt ( $1 * $1 + $2 * $2 ) |
| 67 | EOF |
| 68 | ) |
| 69 | # Setting "scale" to zero rounds down result to integer value, |
| 70 | #+ a necessary compromise in this script. |
| 71 | # It decreases the accuracy of this simulation. |
| 72 | } |
| 73 | |
| 74 | |
| 75 | # ========================================================== |
| 76 | # main() { |
| 77 | # "Main" code block, mimicking a C-language main() function. |
| 78 | |
| 79 | # Initialize variables. |
| 80 | shots=0 |
| 81 | splashes=0 |
| 82 | thuds=0 |
| 83 | Pi=0 |
| 84 | error=0 |
| 85 | |
| 86 | while [ "$shots" -lt "$MAXSHOTS" ] # Main loop. |
| 87 | do |
| 88 | |
| 89 | xCoord=$(get_random) # Get random X and Y coords. |
| 90 | yCoord=$(get_random) |
| 91 | hypotenuse $xCoord $yCoord # Hypotenuse of |
| 92 | #+ right-triangle = distance. |
| 93 | ((shots++)) |
| 94 | |
| 95 | printf "#%4d " $shots |
| 96 | printf "Xc = %4d " $xCoord |
| 97 | printf "Yc = %4d " $yCoord |
| 98 | printf "Distance = %5d " $distance # Distance from |
| 99 | #+ center of lake |
| 100 | #+ -- the "origin" -- |
| 101 | #+ coordinate (0,0). |
| 102 | |
| 103 | if [ "$distance" -le "$DIMENSION" ] |
| 104 | then |
| 105 | echo -n "SPLASH! " |
| 106 | ((splashes++)) |
| 107 | else |
| 108 | echo -n "THUD! " |
| 109 | ((thuds++)) |
| 110 | fi |
| 111 | |
| 112 | Pi=$(echo "scale=9; $PMULTIPLIER*$splashes/$shots" | bc) |
| 113 | # Multiply ratio by 4.0. |
| 114 | echo -n "PI ~ $Pi" |
| 115 | echo |
| 116 | |
| 117 | done |
| 118 | |
| 119 | echo |
| 120 | echo "After $shots shots, PI looks like approximately $Pi" |
| 121 | # Tends to run a bit high, |
| 122 | #+ possibly due to round-off error and imperfect randomness of $RANDOM. |
| 123 | # But still usually within plus-or-minus 5% . . . |
| 124 | #+ a pretty fair rough approximation. |
| 125 | error=$(echo "scale=9; $Pi - $M_PI" | bc) |
| 126 | pct_error=$(echo "scale=2; 100.0 * $error / $M_PI" | bc) |
| 127 | echo -n "Deviation from mathematical value of PI = $error" |
| 128 | echo " ($pct_error% error)" |
| 129 | echo |
| 130 | |
| 131 | # End of "main" code block. |
| 132 | # } |
| 133 | # ========================================================== |
| 134 | |
| 135 | exit 0 |
| 136 | |
| 137 | # One might well wonder whether a shell script is appropriate for |
| 138 | #+ an application as complex and computation-intensive as a simulation. |
| 139 | # |
| 140 | # There are at least two justifications. |
| 141 | # 1) As a proof of concept: to show it can be done. |
| 142 | # 2) To prototype and test the algorithms before rewriting |
| 143 | #+ it in a compiled high-level language. |