summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSt33v <st33v@woodenspoon.net>2013-05-16 18:54:58 +1000
committerSt33v <st33v@woodenspoon.net>2013-05-16 18:54:58 +1000
commit8913938c7339c59d13420ac95ab4351db296d97c (patch)
tree230e857f7b6cbb1299afa0d2cddef3f921d0bcdf
parent43359ec19959420a6fdda08c7458615fad854233 (diff)
Update goat.R
Added in helper fns pr() and junkyard() Added a few more instructions in the comments.
-rw-r--r--goat.R31
1 files changed, 31 insertions, 0 deletions
diff --git a/goat.R b/goat.R
index 2472b78..62f6842 100644
--- a/goat.R
+++ b/goat.R
@@ -3,6 +3,17 @@
# http://en.wikipedia.org/wiki/Monty_hall_problem
# SJP 25 Oct 2007
#
+
+#------------------------ Instructions -----------------
+# Read the wikipedia article for background
+# source this file into R.
+# The game() function is played out, using both strategies, swapping or not swapping
+# After Monty opens one of the doors that you didn't choose and invites you to swap.
+
+# Play around with the game() function,
+# changing the number of trials (iterations) and doors etc.
+# The comments in the code are fairly good (EIIDSSM!)
+#
# consts
NUMDOORS <- 3 #
GOAT <- 0
@@ -24,6 +35,26 @@ allocPrize <- function(d=NUMDOORS){
door[getDoor()] <- PRIZE # put the prize behind a door
return(door)
}
+#######################################################################
+# Get a result based on a probability (default is 0.5)
+# a typical usage is: 'if(pr(x)) then ...
+pr <- function(x = 0.5){
+ if (!length(x)) {return()}
+ if (x<0 || x>1) stop(" prob out of range\n")
+ return(ifelse(rbinom(length(x), 1, x), TRUE, FALSE))
+}
+#######################################################################
+# junkYard() place a silent stop() at the end of a source file
+# in effect, the rest of the file is the junkYard of half-good script
+junkYard <- function(){
+ op <- getOption('show.error.messages') # save settings
+# options(show.error.messages = F) # don't warn
+ cat('called from junkYard()\n')
+# print( parent.frame())
+ try(stop(), silent=T)
+ return('how did this get here?')
+}
+
###############################################################
# game() function