Advertisement
Francoo

Monte Carlo method for Pi

Aug 23rd, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.60 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. cordx=0
  4. cordy=0
  5. truecount=0
  6. totalcount=0
  7. radius=0
  8.  
  9. counts=40000 ## SET HERE NUMBER OF POINTS TO TEST
  10.  
  11. while [ $counts -gt $totalcount ]; do
  12.  
  13.     cordx=$RANDOM
  14.     cordy=$RANDOM
  15.    
  16.     radius=$(echo "sqrt( $cordy ^2 + $cordx ^2)" | bc)
  17.    
  18.     if [ $radius -lt 32768 ]; then
  19.         truecount=$(( $truecount + 1 ))
  20.     fi
  21.    
  22.     totalcount=$(( $totalcount + 1 ))
  23.  
  24. done
  25.  
  26. echo "TOTAL COUNTS: $truecount"
  27. echo "TRUE COUNTS: $totalcount"
  28.  
  29. echo -n "RATIO (percent): "
  30. echo "scale=10; ( $truecount/$totalcount ) * 100" | bc
  31.  
  32. echo -n "Pi: "
  33. echo "scale=10; ( $truecount/$totalcount ) * 4" | bc
  34.  
  35. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement