# The Regnus Scripting Tutorial

## Part 9: Dice Notation Expressions

You might find that it is sometimes useful to generate a number at random. For example:

``` START <Example>While playing Monopoly, I rolled a double-##(1\2\3\4\5\6); on my first turn! ```

As you can see, this is a very limited way to select a random value, and particularly arduous when dealing with a greater range of possibilities, or more complex parameters (for example the results of multiple multi-sided dice rolls when using Regnus to operate random encounter charts in RPGs!)

In order to generate random numerical values more easily, Regnus supports a type of expression which can be used in references much like the group expressions and literal expressions already covered, to generate random numbers and perform basic mathematical operations.

These expressions are enclosed in curly brace characters ("{" and "}"), and form a type of micro-syntax within Regnus, based on the dice notation commonly found in game rulebooks. For example:

``` START <Example>While playing Monopoly, I rolled a double-##{D6}; on my first turn! ```

The letter "D" signifies a dice-roll, and the number directly following it signifies the number of sides of each die rolled. So, "D6" would signify the result of a single six-sided die (thus a value from 1 to 6), and "D10" would signify a single ten-sided die (generating a value from 1 to 10).

If the result of multiple dice is desired, the number of dice can be given before the letter "D":

``` START <Example>While playing Monopoly, I rolled ##{2D6}; on my first turn! ```

In this example, the number generated will be the combined result of two six-sided dice (thus a number from 2 to 12). Equally, "3D6" would generate a number from 3 to 18, and so forth.

Dice notation expressions also allow for simple mathematical operators, so where multiple different dice are required, or a fixed value should be added to a dice-roll, these can be added together. For example:

``` START <Example 1>I rolled a six-sided die and a ten-sided die, and scored ##{D6+D10};! START <Example 2>I rolled a six-sided die and added three, and scored ##{D6+3};! ```

Other operators currently supported are subtraction, multiplication, division and exponentiation. For example:

``` START <Subtraction>Two 6-sided dice, subtracting one from the result: ##{2D6−1}; START <Multiplication>A 10-sided die, multiplying the result by ten: ##{D10×10}; START <Division>Two 12-sided dice, dividing the result by two: ##{2D12÷2}; START <Exponentiation>A 6-sided die, squaring the result: ##{D6^2}; ```

Note: Regnus dice notation expressions accept a variety of different operator characters! Subtraction can be performed using either the minus sign character ("−") or the hyphen character ("-"); multiplication using the multiplication symbol ("×"), the asterisk symbol ("*") or the letter "X" ("x" or "X"); division using the obelus symbol ("÷"), forward slash ("/") or backslash ("\") characters; addition using the plus sign ("+"); and exponentiation using the circumflex character ("^"). As a general rule for multiplication, it is generally recommended to avoid using the asterisk character, in order to avoid confusion with its use elsewhere in Regnus as an escape character.

Note: When using division in a dice notation expression, the result is always rounded down to the nearest integer.

It is also possible to use parantheses ("(" and ")") to bracket parts of mathematical expressions if more complex results are required:

``` START <Example 1>One six-sided die, subtracting one from the result, and then multiplying by two: ##{2×(D6−1)}; START <Example 2>Two six-sided dice, adding one to each result, and then multiplying the two results: ##{(D6+1)×(D6+1)}; ```

Note: Unlike in the current convention for standard mathematical notation and most other programming languages, expressions are resolved strictly from left to right, with no operator priority; thus, the expression "2+1×3" is interpreted as "(2+1)×3" rather than as "2+(1×3)", as might usually be expected - so do use brackets to specify order of precedence where necessary!