Skip to content

Instantly share code, notes, and snippets.

@thedavecarroll
Last active April 27, 2021 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedavecarroll/1343177f60d73cdd07e19e4afef74de1 to your computer and use it in GitHub Desktop.
Save thedavecarroll/1343177f60d73cdd07e19e4afef74de1 to your computer and use it in GitHub Desktop.
Simple Dice Roller, a la D&D
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