knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "80%"
)

RollR

Lifecycle: experimental

This R package provides a simple way to make all sorts of dice rolls using syntax inspired from tabletop roleplaying games like Dungeons & Dragons.

Installation

# install.packages("remotes")
remotes::install_github("felixmil/rollr")

Examples

library(rollr)

roll_dice("1d12") # rolls one 12-sided dice

roll_dice("1d20+2") # roll one 20-sided dice then adds 2

roll_dice("2d10 + 1d4") # rolls two 10-sided dice and one 4-sided diced and sums their results 

roll_dice("4d6h3") # rolls four 6-sided dice and sum the 3 highests

roll_dice("2d20l1") # rolls two 20-sided dice and keeps the lowest one

Use Case

in Dungeon & Dragons 5th Edition, ability scores are set by rolling 4d6 (four 6-sided dice) and keeping the 3 highest ones (ability scores can range between 3 and 18).

Let's say we want to create a new character: Dunrill, a strong and muscular Dwarf. We roll our first set of 4 6-sided dice and keep the 3 highests.

library(rollr)

set.seed(42)

roll_results <- roll_dice("4d6h3", roll_history = TRUE) 

That's a 7. Now, the same roll (4d6h3) must be reproduced 5 more times:

more_roll_results <- replicate(5, roll_dice("4d6h3"))

all_results <- c(roll_results, more_roll_results)

print(all_results)

Now, we can assign these 6 values to Dunrill abilities (Strength, Dexterity, Constitution...) move on with character creation.

But were we lucky rolling these values: r paste(all_results, collapse=", ") ?

To know that, we'll simulate 9999 4d6h3 rolls and approximate the probability distribution.

random_rolls <- replicate(9999, roll_dice("4d6h3"))

Probability distribution for 4d6h3 looks like this:

library(magrittr)
library(dplyr)
library(ggplot2)
library(emojifont)

mode = tibble(r = random_rolls) %>%
  group_by(r) %>% 
  count() %>% 
  ungroup() %>% 
  filter(n == max(n)) %>% 
  pull(r)

mean = round(mean(all_results),2)
plot <- random_rolls %>%
  tibble(r = .) %>%
  ggplot(data=., aes(x=r)) +
  geom_density(color="black",size=2,bw=1)+
  geom_histogram(aes(y=..density..), 
                 position='identity',
                 binwidth = 1,
                 fill="#AF0000", 
                 color="white",
                 alpha=0.8)+
  geom_vline(xintercept = mode,
             color="white", linetype="dashed", size=2)+
  scale_x_continuous(breaks = seq(3,18,1))+
  scale_y_continuous(expand = c(0,0))+
  labs(title = "Probability distribution for 4d6h3 dice roll", 
       x= "roll result",
       y="probability density")+
  theme_classic()+
  theme(text = element_text(size=14),
        plot.title = element_text(hjust = 0.5, size=14,face = 'bold'))

plot +
  geom_label(data = tibble(x=1), x=15.5, y = 0.11, label = '50% chance to get\n a score above 13',size=4, hjust=0)+
  geom_label(data = tibble(x=1),x=9.5, y = 0.11, label = '50% chance to get\n a score below 13',size=4, hjust=1)

Distribution's mode is r mode, however, the mean of our rolls is r mean:

emojifont::load.emojifont()
plot +
  geom_text(data= tibble(x=1),label= ":(", x= mean, y = 0.05, size=6)+
  geom_segment(xend=1.05*mean, x=0.98*mode,y=0.05, yend=0.05,arrow = arrow(type = "closed"))+
  labs(title="Our luck was below average...")

Rolls syntax (supported dice rolls)

Features list derivated from Sidekick

TO DO



Felixmil/rollR documentation built on July 11, 2020, 2:36 a.m.