powershell - 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 的工作原理并为我的循环变为无限的原因提出解决方案吗?
解决方案
这是另一种使用列表且不需要重新滚动重复项的方式:
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
推荐阅读
- python - 我刮掉了标题和价格以及链接和信息表,我将其命名为planet_data,当我编写csv文件时,我得到了重复的planet_data
- sas - 如何从我的表 SAS 4GL - RETAIN 创建变量列?
- java - 对于本机查询,Oracle 函数调用有时会失败
- c# - Cookie 不会根据 StartUp.cs 设置过期
- azure - 将自定义维度添加到请求遥测 - Azure 函数
- python - 无法导入 tf_sentencepiece - 原因:找不到图像
- c# - 在 .NET Framework 中使用 Autofac 库的问题
- instagram - 我无法通过 Facebook Graph api 获取 Instagram 企业帐户 ID
- php - 如何将自定义数据显示到 Tcpdf 的头文件中?
- javascript - 我们安装 webpack 4 时是否安装了 babel 和 uglify.js