The Basics
AnyDice can perform mathematical operations on numbers, like any regular calculator can.
- Addition
1 + 2
yields3
.- Subtraction
3 - 2
yields1
.- Multiplication
2 * 3
yields6
.- Integer division
- As AnyDice only supports whole numbers, all divisions are integer divisions, rounding toward zero. So
6 / 3
yields2
, while5 / 3
yields1
. - Exponentiation
2 ^ 3
yields8
.
-3
). After that, it does
exponentiations, then multiplications and divisions, followed by additions and subtractions. It performs operations of the same precedence in the order
it encounters them, from left to right. You can use parentheses to change order in which the operations are performed.
Comments
Any text enclosed with a backslash character on either end –\ like this \
– will be ignored by AnyDice. This is useful
for writing explanations or for temporarily disabling some commands.
Variables
Variables are uppercase words, which may contain underscores. It is possible to assign something to a variable, so that you can reuse it later. Assigning something to a variable will replace its previous value.X: 3
output X named "It's a 3!"
output X + 1 named "It's a 4!"
X: X * 2
output X named "It's a 6!"
Conditions
AnyDice allows you to compare two values, to check whether a certain condition is true. If so, the result will be a1
,
otherwise a 0
. Condition operations have a lower precedence than all mathematical operations.
- Equal?
2 = 2
yields1
, while1 = 2
yields0
.- Not equal?
1 != 2
yields1
, while2 != 2
yields0
.- Smaller?
1 < 2
yields1
, while2 < 2
and3 < 2
both yield0
.- Greater?
3 > 2
yields1
, while2 > 2
and1 > 2
both yield0
.- At least?
2 >= 2
and3 >= 2
both yield1
, while1 >= 2
yields0
.- At most?
1 <= 2
and2 <= 2
both yield1
, while3 <= 2
yields0
.
Booleans
In general, the number0
represents no while any other number – either positive or negative – represents yes.
Using this logic, AnyDice allows you to perform boolean operations. This is useful if you like to check multiple conditions. Boolean operations
have a lower precedence than all conditions, except for Not, which is a unary operation.
In the examples below, assume that the variable YES
is set to any number other than 0
, while NO
is set to 0
.
- And also
YES & YES
yields1
, while any other combination with aNO
yields0
.- Or else
NO | NO
yields0
, while any other combination with aYES
yields1
.- Not
!YES
yields0
, while!NO
yields1
.