首页 > 解决方案 > D&D 骰子模拟器

问题描述

我想出了一个滚动策略,您可以滚动 4d6 并重新滚动任何重复项。这导致数字介于 10 和 18 之间,这导致 14 是最常见的,而 10 和 18 是最不常见的。要指定,您保留 1 个重复项并重新滚动其他项。而且我在任何骰子滚动应用程序/在线上都找不到这种方法。我正在尝试在 PowerShell 中运行:

$A = Get-Random -Maximum 6 -Minimum 1
$B = Get-Random -Maximum 6 -Minimum 1
$C = Get-Random -Maximum 6 -Minimum 1
$D = Get-Random -Maximum 6 -Minimum 1

While ($A = $B) {
$B = Get-Random -Maximum 6 -Minimum 1
}
... 
...

$Stat = $A+$B+$C+$D

但是 $A 和 $B 总是相同的,而 $C 和 $D 总是相同的。任何人都可以解释 Get-Random 的工作原理并为我的循环变为无限的原因提出解决方案吗?

标签: powershellrandom

解决方案


这是另一种使用列表且不需要重新滚动重复项的方式:

Function Roll {

    # Die face values
    $roll = [System.Collections.Generic.List[int]]@(1..6)
    # Perform the required number of rolls
    $values = 1..4 | Foreach-Object { 
        # Random roll
        $r = Get-Random -InputObject $roll
        # Remove rolled value from list to prevent duplicates
        [void]$roll.Remove($r)
        # return roll
        $r
    }
    # Sum rolls
    ($values | Measure-Object -Sum).Sum
}

# Execute the rolls
Roll

# Output
16

推荐阅读