AnyDice Classic Archive 22

AnyDice 2 syntax basics

Wed, 06 Jan 2010 00:00:00 +0000

While AnyDice 1 is a single-statement calculator, AnyDice 2 is a multi-statement interpreter. It has useful features like variables and custom functions. I'll demonstate some of the new syntax, pretending for the moment that all it can do is manipulate numbers, not dice.

Generating output

output 42
The simplest thing to do is simply output something. If you want to, you could give it a label.
output 42 as "the Answer to the Ultimate Question"
Of course, most mathematical operations are available.
output (2 + 2 ^ 2) * 7

Variables

X: 7
Y: 2 + 2 ^ 2
output X * Y
You define a variable using a colon. Variables are uppercase words. So whenever you encounter an uppercase word, you know it's a variable.

Functions

output [40 plus 2]
A function call is anything within square brackets. Syntactically, it consists of a collection of lowercase words and function arguments. There are no other restrictions.
function: A plus B {
    result: A + B
}
output [20 plus [20 plus 2]]
You define a function as if you assign it to the special variable "function". You declare its signature as a lowercase phrase containing argument placeholders. After that comes the code that belongs to the function, within braces. You define the result of the function by declaring the special "result" variable, which ends execution of the function.

Here's a more complex example.

function: factorial of N {
    if N < 3 { result: N }
    result: N * [factorial of N - 1]
}
output [factorial of 10]
If you're curious, it's 3,628,800.

It's all about the dice

If this is all that AnyDice 2 can do, then what's the point? Why not use Python or any other high-level programming language? The point is that this is indeed what it can do, but with dice, without adding complexity for the user. I'll explore what that entails in a future blog post.
comments are closed