Skip to content

Instantly share code, notes, and snippets.

@dhicks
Created October 16, 2023 05:16
Show Gist options
  • Save dhicks/7f1883ada33b64f62b64a0915fe5ee9a to your computer and use it in GitHub Desktop.
Save dhicks/7f1883ada33b64f62b64a0915fe5ee9a to your computer and use it in GitHub Desktop.
Rolling elven accuracy using RAW vs. just rolling all three at once
library(magrittr)
## Given a vector, calculate both the mean and standard deviation
mean_sd = function(vec) {
list(mean = mean(vec),
sd = sd(vec))
}
d20 = function() sample.int(20, 1)
## RAW
## Roll 2; keep the larger; roll 1 more; take the max
roll_raw = function(x) {
c(d20(), d20()) %>%
max() %>%
c(., d20()) |>
max()
}
## Roll 3
## Roll 3 at once and take the max
roll_three = function(x) max(c(d20(), d20(), d20()))
## How many times to run the simulation?
N = 10000
set.seed(2023-10-15)
## Run the simulation and report the mean value
## RAW
## $mean
## [1] 15.4533
## $sd
## [1] 3.901864
sapply(1:N, roll_raw) |>
mean_sd()
## Just roll 3 at once
## $mean
## [1] 15.5064
## $sd
## [1] 3.853368
sapply(1:N, roll_three) |>
mean_sd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment