Skip to content

Commit

Permalink
first version of montyhall.rkt
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Williamson committed Sep 6, 2017
1 parent dfa67ad commit 8c2dd9a
Show file tree
Hide file tree
Showing 5 changed files with 1,124 additions and 65 deletions.
2 changes: 2 additions & 0 deletions montyhall.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#lang sicp

82 changes: 82 additions & 0 deletions montyhall.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#lang racket

(define num-doors 3)

(define (num-to-list n)
(define (to-list-rec c n)
(if (> c n)
(quote())
(cons c (to-list-rec (+ c 1) n))))
(to-list-rec 1 n))

;this is a test. all comments are function tests
;(num-to-list 3)

(define (remove-door n list)
(cond
((null? list)(quote()))
((= (car list) n)(remove-door n (cdr list)))
(else(cons (car list)(remove-door n (cdr list))))))


;(pick 1 '(alpha delta doggmega))
;(pick 2 '(alpha delta doggmega))
;(pick 3 '(alpha delta doggmega))

(define (a-trial)

(define prize-door
(+ 1 (random num-doors)))

(define first-pick
(+ 1 (random num-doors)))
(newline)
(newline)
(display "prize door: ")
(display prize-door)

;to remove unnecessary trials where 'player' selects correct door or a door with a prize is revealed remove these from initial list
(define doors
(num-to-list num-doors))
(newline)
(display "initial pick: ")(display first-pick)
;get a wonk, a door that isn't the prize door and isn't the initial door. There will always be one more door that is a wonk
;regardless of whether initial pick is correct or not.
(define wonk (car(remove-door first-pick(remove-door prize-door doors))))

(newline)
(display "wonk: ")
(display wonk)

(define remaining-doors (remove-door wonk doors))

(newline)
(display "remaining doors: ")
(display remaining-doors)
;switch doors; this is where most people assume there is a 50/50 chance
(define second-pick (car(remove-door first-pick remaining-doors)))
(newline)
(display "second pick: ")
(display second-pick)

(if(= prize-door second-pick)
true
false)
)

(define (monty-hall n)
(define total-trials n)
(define (monty-hall-iter n right wrong)
(cond
((= n 0)(newline)(newline)(display "ratio correct: ")(display (/ right total-trials)))
((a-trial)(newline)(display "correct")(monty-hall-iter (- n 1) (+ 1 right) wrong))
(else (newline)(display "incorrect")(monty-hall-iter (- n 1) right (+ 1 wrong)))))
(monty-hall-iter n 0 0))

;run simulation 1000 times
(monty-hall 1000)

;(remove-door 1 (num-to-list 3))
;(remove-door 2 (num-to-list 3))
;(remove-door 3 (num-to-list 3))

Loading

0 comments on commit 8c2dd9a

Please sign in to comment.