Skip to content

Instantly share code, notes, and snippets.

@nutterb
Created December 5, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutterb/0433257bb6160e92bbd5a7768fc76c7b to your computer and use it in GitHub Desktop.
Save nutterb/0433257bb6160e92bbd5a7768fc76c7b to your computer and use it in GitHub Desktop.
risk_attack_succeed_sim <- function(attack, defend){
while(attack >= 2 && defend > 0){
n_attack <- min(c(3, attack - 1))
n_defend <- min(c(2, defend))
roll_attack <- sample(1:6, n_attack, replace = TRUE)
roll_defend <- sample(1:6, n_defend, replace = TRUE)
n_compare <- min(c(n_attack, n_defend))
roll_attack <- sort(roll_attack, decreasing = TRUE)[seq_len(n_compare)]
roll_defend <- sort(roll_defend, decreasing = TRUE)[seq_len(n_compare)]
win_defend <- sum(roll_attack <= roll_defend)
attack <- attack - (win_defend)
defend <- defend - (n_compare - win_defend)
}
defend == 0
}
risk_prob_win <- function(attack, defend, rep){
sum(replicate(rep, risk_attack_succeed_sim(attack, defend))) / rep
}
risk_prob_win(2, 1, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment