We're Hiring!
Whitepaper
Docs
Sign In
@did100
·
8 months ago
tool
RPG Dice Roller
Get
Last Updated
8 months ago
Created
8 months ago
Tool
Name
RPG Dice Roller
Downloads
135+
Saves
0+
Description
Tool to simulate virtual dice rolls using syntax: XDY ± Z, where X is the number of dice, Y is the number of sides per die, and ±Z (optional) is a constant to add or subtract.
Tool Code
Show
""" Tool to simulate virtual dice rolls. Syntax: XDY ± Z - X: Number of dice - D: Literal letter separating number of dice from number of sides - Y: Number of sides per die - ±Z (optional): Constant to add or subtract from the total result Author: did100 """ import random class Tools: def __init__(self): self.citation = True pass def rpg_dice_roll(self, dice_expression: str) -> str: """ Rolls a given number of dice with a given number of sides and an optional modifier. The expression should be in the format 'XdY' or 'XdY+Z' or 'XdY-Z'. X is the number of dice, Y is the number of sides per die, Z is the modifier (optional). """ import re # Regex to parse the dice expression match = re.match(r"(\d+)D(\d+)([+-]?\d*)", dice_expression.upper()) if not match: return "Invalid dice expression. Please use the format 'XdY' or 'XdY+Z' or 'XdY-Z'." num_dice = int(match.group(1)) num_sides = int(match.group(2)) modifier_str = match.group(3) modifier = int(modifier_str) if modifier_str else 0 if num_dice <= 0 or num_sides <= 0: return "The number of dice and sides must be greater than zero." results = [random.randint(1, num_sides) for _ in range(num_dice)] total = sum(results) + modifier return f"You asked for {dice_expression}. The dice rolled: {results}. Total with modifier ({modifier}): {total}." # Exemple d'utilisation tools = Tools() print(tools.rpg_dice_roll("3D8+2")) print(tools.rpg_dice_roll("3D8-5"))
Sponsored by Open WebUI Inc.
We are hiring!
Shape the way humanity engages with
intelligence
.