Created
December 5, 2017 20:04
-
-
Save nutterb/0433257bb6160e92bbd5a7768fc76c7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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