Advertisement
metalx1000

BASH - The Monty Hall Problem v1

Apr 30th, 2013
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. let st=0
  4. let sw=0
  5.  
  6. for i in {1..100}
  7. do
  8.     #create 3 doors
  9.     door[1]="win"
  10.     door[2]="lose"
  11.     door[3]="lose"
  12.  
  13.     x=$((RANDOM % 3 + 1)) #pick a random door
  14.     p=${door[$x]} #save players choice
  15.     door=(${door[@]:0:$x} ${door[@]:$(($x + 1))}) #remove players choice from array
  16.  
  17.     #check remaining 2 doors
  18.     a=${door[1]}
  19.     b=${door[2]}
  20.     #if either door is a winner switching will win
  21.     if [ "$a" = "win" ] || [ "$b" = "win" ]
  22.     then
  23.         z="win"
  24.         let sw+=1
  25.     else
  26.         z="lose" #if both are lose then switching will lose
  27.         let st+=1
  28.     fi
  29.  
  30.     #display out come
  31.     echo "$p -- $z"
  32. done
  33.  
  34. echo "switching wins $sw% of the time"
  35. echo "staying wins $st% of the time"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement