Last active
April 27, 2021 21:09
-
-
Save thedavecarroll/1343177f60d73cdd07e19e4afef74de1 to your computer and use it in GitHub Desktop.
Simple Dice Roller, a la D&D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-DiceRoll { | |
[Alias('idr','roll')] | |
[CmdletBinding()] | |
param( | |
[ValidatePattern( | |
'^(?:[1-9]|0[1-9]|1[0-9]|20)d(4|6|8|12|20|30|100)$', | |
ErrorMessage='Valid die types are d4,d6,d8,d10,d12,d20,d30,d100 rolled beteen 1 and 20 times. Your input was {0}. Please try again.' | |
)] | |
[string]$Roll = '2d6', | |
[switch]$Total | |
) | |
[int]$Rolls,[int]$Sides = $Roll.Split('d') | |
$DiceRolls = for ($i = 1 ; $i -le $Rolls; $i++) { | |
Get-Random -Minimum 1 -Maximum ($Sides + 1) | |
} | |
if ($PSBoundParameters.ContainsKey('Total') -and $Rolls -ne 1) { | |
($DiceRolls | Measure-Object -Sum).Sum | |
} else { | |
$DiceRolls | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment