Skip to content

Instantly share code, notes, and snippets.

@phihag
Last active July 23, 2016 09:55
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 phihag/765e81782126a6bc73e1510d33edff0a to your computer and use it in GitHub Desktop.
Save phihag/765e81782126a6bc73e1510d33edff0a to your computer and use it in GitHub Desktop.
Simulate a Badminton game
PROB <- 0.5
RUNS <- 10000
game_is_over <- function(p, q) {
return((
((p == 21) && (q < 20)) ||
((p > 21) && (p <= 30) && (p - 2 == q)) ||
(p == 30) && (q == 29)
) || (
((q == 21) && (p < 20)) ||
((q > 21) && (q <= 30) && (q - 2 == p)) ||
(q == 30) && (p == 29)
))
}
sim_game <- function(prob) {
points <- c(0, 0)
while (! game_is_over(points[1], points[2])) {
rally_winner = sample(1:2, 1, TRUE, c(prob, 1 - prob))
points[rally_winner] <- points[rally_winner] + 1
}
return(points)
}
sim_once <- function() {
games <- c(0, 0)
points <- c(0, 0)
while (max(games) < 2) {
# different probabilities on different sides
#prob <- if(sum(games) == 2) 0.5 else (if(sum(games) == 1) (PROB) else (1 - PROB))
game <- sim_game(PROB)
winner <- if (game[1] > game[2]) 1 else 2
games[winner] <- games[winner] + 1
points <- points + game
}
match_winner <- if (points[1] > points[2]) 1 else 2
return(list(points=points, match_winner=match_winner))
}
sim <- function(match_count) {
points <- c(0, 0)
won_matches <- c(0, 0)
for (m in 1:match_count) {
res <- sim_once()
points <- points + res[['points']]
match_winner <- res[['match_winner']]
won_matches[match_winner] <- won_matches[match_winner] + 1
}
winrate <- won_matches[1] / match_count
avg_points <- sum(points) / match_count
return(list(avg_points=avg_points, winrate=winrate))
}
print(sim(RUNS))
RUNS <- 10000
game_is_over <- function(p, q) {
return((
((p == 21) && (q < 20)) ||
((p > 21) && (p <= 30) && (p - 2 == q)) ||
(p == 30) && (q == 29)
) || (
((q == 21) && (p < 20)) ||
((q > 21) && (q <= 30) && (q - 2 == p)) ||
(q == 30) && (p == 29)
))
}
sim_game <- function() {
points <- c(0, 0)
winchances <- seq(0.5, 0.9, 0.05)
last_winner <- 0
won_count <- 0
while (! game_is_over(points[1], points[2])) {
wwc <- winchances[won_count + 1]
wwc <- if (is.na(wwc)) tail(winchances, 1) else wwc
prob <- if (last_winner == 1) wwc else 1 - wwc
rally_winner <- sample(1:2, 1, TRUE, c(prob, 1 - prob))
won_count = if (rally_winner != last_winner) 0 else won_count + 1
last_winner = rally_winner
points[rally_winner] <- points[rally_winner] + 1
}
return(points)
}
sim_once <- function() {
games <- c(0, 0)
points <- c(0, 0)
while (max(games) < 2) {
game <- sim_game()
winner <- if (game[1] > game[2]) 1 else 2
games[winner] <- games[winner] + 1
points <- points + game
}
match_winner <- if (points[1] > points[2]) 1 else 2
return(list(points=points, match_winner=match_winner))
}
sim <- function(match_count) {
points <- c(0, 0)
won_matches <- c(0, 0)
for (m in 1:match_count) {
res <- sim_once()
points <- points + res[['points']]
match_winner <- res[['match_winner']]
won_matches[match_winner] <- won_matches[match_winner] + 1
}
winrate <- won_matches[1] / match_count
avg_points <- sum(points) / match_count
return(list(avg_points=avg_points, winrate=winrate))
}
print(sim(RUNS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment