roll_dice
Roll dice using standard notation (e.g., '2d6+4') to simulate random outcomes for games or probability tasks. Specify notation and number of rolls to generate results.
Instructions
Roll the dice with the given notation
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| notation | Yes | ||
| num_rolls | No |
Implementation Reference
- server.py:19-23 (handler)MCP tool handler for 'roll_dice' - registers with @mcp.tool() decorator, creates a DiceRoller instance and returns the string representation of the roll result.
@mcp.tool() def roll_dice(notation: str, num_rolls: int = 1) -> str: """Roll the dice with the given notation""" roller = DiceRoller(notation, num_rolls) return str(roller) - server.py:19-23 (registration)Tool registration via FastMCP's @mcp.tool() decorator, exposing the roll_dice function as an MCP tool with parameters 'notation' (required string) and 'num_rolls' (optional int, default 1).
@mcp.tool() def roll_dice(notation: str, num_rolls: int = 1) -> str: """Roll the dice with the given notation""" roller = DiceRoller(notation, num_rolls) return str(roller) - dice_roller.py:10-23 (helper)Core helper that implements the dice rolling logic using Python's random module. Parses notation (e.g., '2d20k1'), generates random rolls, sorts them descending, and keeps the top N results.
def roll_dice(self): match = self.dice_pattern.match(self.notation) if not match: raise ValueError("Invalid dice notation") num_dice = int(match.group(1)) dice_sides = int(match.group(2)) keep = int(match.group(4)) if match.group(4) else num_dice rolls = [random.randint(1, dice_sides) for _ in range(num_dice)] rolls.sort(reverse=True) kept_rolls = rolls[:keep] return rolls, kept_rolls - dice_roller_numpy.py:10-24 (helper)Alternative helper that implements dice rolling using numpy for random generation. Same logic but uses np.random.randint instead of random.randint.
def roll_dice(self): match = self.dice_pattern.match(self.notation) if not match: raise ValueError("Invalid dice notation") num_dice = int(match.group(1)) dice_sides = int(match.group(2)) keep = int(match.group(4)) if match.group(4) else num_dice # Use numpy to generate random integers rolls = np.random.randint(1, dice_sides + 1, size=num_dice).tolist() rolls.sort(reverse=True) kept_rolls = rolls[:keep] return rolls, kept_rolls