AnyDice Classic Archive 23
Mixing numbers and dice
In my previous blog post I wrote about the basic syntax of AnyDice 2. I showed how to compute stuff and create custom functions, while restricting myself to numbers only. I refrained from using dice for the sake of simplicity.
So, how do you add dice? Easy! You can use dice almost everywhere you can use normal numbers, and it'll just work.
output (3d6 + 2) * 1d4
It works just as you would expect.
Custom Dice
Custom dice are also back, and better than ever! You can do the same stuff as AnyDice 1 supports – though with a slightly changed syntax – plus more. Here are some examples.output d{1,2,3,4} as "d4"
output d{2,3,3,4} as "2d2"
output d{1:4,5,6} as "d{1,1,1,1,5,6}"
output d{-1..1} as "Fudge die"
output d{d4,d6} as "flip a coin, then roll either d4 or d6"
output d{d4..d4} as "d{1,1,2,1,2,3,1,2,3,4,2,2,3,2,3,4,3,3,4,4}"
Functions with typed parameters
Functions simply pass along anything you throw at them.function: A plus B {
result: A + B
}
output [1d20 plus 6]
However, you might want your function to exclusively work with numbers, not dice. You can do this by constraining the function's parameters to a type, in this case "i" for integer.
function: A:i plus B:i {
result: A + B
}
output [1d20 plus 6]
So what happens when you feed a die to a function, when it demands a number instead? It will be executed once for each value of the die, and the results will be collected in a new distribution. In other words, it loops over all elements of the die.
You can do a lot of fun things using this approach. For example, create a die with the values 1!, 2!, ..., 10!.
function: factorial of N:i {
if N < 3 { result: N }
result: N * [factorial of N - 1]
}
output [factorial of 1d10]
Another example could be to first roll a d4, then roll either a d4, a d6, a d8, or a d10 and keep its results, depending on what the d4 rolled.
function: choose a die based on N:i {
if N = 1 { result: 1d4 }
if N = 2 { result: 1d6 }
if N = 3 { result: 1d8 }
if N = 4 { result: 1d10 }
}
output [choose a die based on 1d4]
By the way, this is the same as the custom die d{d4,d6,d8,d10}.
This ain't all folks
Using what I've shown so far, AnyDice 2 can already do quite a bit more than AnyDice 1. But there are still more features I haven't shown yet. I'll write about that later.comments are closed