```
# Author: Dason Kurkiewicz
# May 30, 2013
# http://www.talkstats.com/showthread.php/44051-Radio-Competition-HELP-NEEDED!
# Function to perform the optimal guessing strategy
optimalguess <- function(lower, upper){
# optimal strategy
round(mean(c(lower, upper)))
}
# Function that guesses uniformly amoung the possible values
randomguess <- function(lower, upper){
# not so great
sample(seq(lower, upper), 1)
}
# Function that guess uniformly amoung only the middle portion
# of the remaining possible values
randommiddle <- function(lower, upper){
middle <- round(mean(c(lower, upper)))
int <- round((upper - lower)/4) # middle half of possible values
vals <- seq(middle - int, middle + int)
if(length(vals) == 1){return(vals)}
sample(vals, 1)
}
# guessmethod: function that takes two parameters (lower, upper) and returns
# a next guess
# lower : numeric - lower boundary for possible values (in cents)
# upper : numeric - upper boundary for possible values (in cents)
simulateCompetition <- function(guessmethod = optimalguess, lower = 2000000, upper = 9999999){
actual <- sample(seq(lower, upper), 1)
i <- 1
guess <- guessmethod(lower, upper)
while(guess != actual){
i <- i + 1
if(guess < actual){
lower <- guess
}else{
upper <- guess
}
guess <- guessmethod(lower, upper)
}
i
}
N <- 1000
optimal <- replicate(N, simulateCompetition(optimalguess))
randommiddle <- replicate(N, simulateCompetition(randommiddle))
random <- replicate(N, simulateCompetition(randomguess ))
summary(optimal)
summary(randommiddle)
summary(random)
```