Skip to content

Instantly share code, notes, and snippets.

@haroldkyle
Last active August 29, 2015 14:16
Show Gist options
  • Save haroldkyle/807867c349e8af75ecd9 to your computer and use it in GitHub Desktop.
Save haroldkyle/807867c349e8af75ecd9 to your computer and use it in GitHub Desktop.
# namespace gl
gl <- new.env()
# # of trials to run
gl$trials <- 20000
# profits (price minus costs) and their related probabilities
gl$entreeProfit <- c(9,7.5,5.5,4)
gl$entreeProbability <- c(0.25,0.35,0.3,0.1)
# empty vector for trial data
gl$results <- c()
# iterate through trials and fill data frame row by row
for(i in 1:gl$trials){
profit <- sample(gl$entreeProfit,1,TRUE,gl$entreeProbability)
meals <- rnorm(1,3000,1000)
labor <- runif(1,5040,6860)
gl$results <- rbind(gl$results, profit * meals - labor - 3995)
}
# tidy up global environment
remove(profit,meals,labor,i)
summary(gl$results)
hist(gl$results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment