Average Roll Breakdown by Attribute Dice

An accurate openlegend function for anydice.com: http://anydice.com/program/cde0

The outputs there default to showing Disadvantage 1 for all attribute levels, but you can change the outputs at the bottom of the script to compare any rolls you want. Click the [Graph] and [At Least] buttons for a good comparative view.

SYNTAX: [openlegend attribute_level {advantage_level}]
where attribute_level is a value 0 - 9
and advantage_level is the number for level of advantage or disadvantage (use negative values for disadvantage)

It was tricky to get the dice rules exactly right since you have to select the dice results before exploding, and if you try something like [explode [highest 1 of 2d20]], then on a 20 the explode function rolls another [highest 1 of 2d20] instead of just another 1d20.

It took me a while to figure out how to pass a sequence of dice results to a function as a parameter. The key is that if the function is expecting a parameter of type Sequence and you pass it a set of dice (eg 3d6), the anydice engine will call your function with every possible sequence of results for those dice, sorted highest to lowest. Then it’s easy to iterate across the correct subset of dice, check for explosions, and sum it all up.

function: openlegendhelper SEQ:s D:n START:n END:n {
 RES: 0
 loop I over {START..END} {
  RES: RES + I@SEQ
  if I@SEQ = D { RES: RES + [explode 1dD] }
 }
 result: RES
}

function: openlegend LEV:n ADV:n {
 N: 1
 if LEV = 0 {
  D: 20
  RES: 0
 }
 else {
  RES: [explode 1d20]
  if LEV > 4 {
   if LEV > 7 { 
    N: 3
    D: (LEV*2)-8
   }
   else { 
    N: 2
    D: (LEV*2)-4
   }
  }
  else { D: (LEV*2)+2 }
 }
 if ADV < 0 {
  N: N-ADV
  START: 1-ADV
  END: N
 }
 else {
  START: 1
  END: N
  N: N+ADV
 }
 result: RES + [openlegendhelper NdD D START END]
}

[Cross-posted from here: Accurate openlegend dice roll function for anydice.com]

2 Likes