Roll Dungeons and Dragons dice I want to play Dungeons and Dragons, but I don't have any dice! Your challenge is to roll some D&amp;D dice.

The input format specification in Backus-Naur form is:

&lt;valid-input&gt;  ::= &lt;opt-integer&gt; "d" &lt;integer&gt; &lt;opt-modifier&gt;
&lt;opt-integer&gt;  ::= | &lt;integer&gt;
&lt;opt-modifier&gt; ::= | "+" &lt;integer&gt;
&lt;integer&gt;      ::= "0" | "1" | "2" | "3" | "4" | "5" |
                   "6" | "7" | "8" | "9" | &lt;integer&gt; &lt;integer&gt;


The optional integer before the d is the number of dice to roll; it must be at least 1, and defaults to 1 if not supplied.

The required integer immediately after the d is the number of sides each die has; it must be at least 1. The sides of each die are distinct consecutive positive integers starting at 1.

The optional modifier may be +0, and it defaults to +0 if not specified.

For example, for the input 2d10+5, you generate two random numbers from 1 to 10 inclusive, add them together, and add 5. Then you will output the result.

If you receive invalid input, such as 2d, d20+, 0d4, 2d5+1+2, 2+2, or anything else that doesn't fit this format, you must output "Invalid input". Otherwise, you must output only a single random integer, weighted according to the input. For example, 3d6 should produce more 10s than 4s.

Test cases

Input      Minimum possible output    Maximum possible output
d1         1                          1
d6         1                          6
d1+3       4                          4
d20+3      4                          23
2d1        2                          2
2d6+2      4                          14
d01        1                          1
d01+0      1                          1
01d01+01   2                          2
3d20+10    13                         70

d          Invalid input
d0         Invalid input
d+0        Invalid input
d0+0       Invalid input
0d1        Invalid input
0d1+1      Invalid input
d1+        Invalid input
1d         Invalid input
1d1+       Invalid input
1d+1       Invalid input
2d+2d      Invalid input
d2+d2      Invalid input
d2+2+2     Invalid input
d2-1       Invalid input
-d2        Invalid input
-2d2       Invalid input
4*3        Invalid input
4*d2       Invalid input


This is code-golf, so the shortest code in bytes will win!
Perl, 109 95 93 96 89 bytes

s/^d/1d/;/^(\d+)d(\d+)(\+\d+)?$/;$d+=1+rand$2|0for
1..$1;$_=$1*$2?$d+$3:'Invalid input'


Requires the -p switch, which accounts for two of the bytes. Try it online on Ideone.

How it works


Because of the -p switch, a line is read from STDIN and stored in $_.
The command s/^d/1d/ prepends a 1 to $_ if it begins with a d, i. e., if the number of dice has not been specified.
The regular expression /^(\d+)d(\d+)(\+\d+)?/ checks if the line consists of a number, a literal d, another number and, optionally, a third number preceded by a + sign.

If there is a match, the numbers will be saved in $1, $2 and $3.

In this case, the input will be valid if and only if $1 and $2 are both positive.
$d += 1 + rand $2 | 0 adds a pseudo-randomly chosen integer from 1 to the specified number of sides to $d (initially treated as zero).
for 1 .. $1 does the above once for every integer between 1 and the number of dice.
The command $_ = $1 * $2 ? $d + $3 : 'Invalid input' does the following:


If $1 * $2 is zero, it sets $_ to Invalid input.
Otherwise, the input is valid and it sets $_ to the sum of the dice rolls and the modifier.

Because of the -p switch, Perl prints the contents of $_.
Since there a no further input lines, the script exits.