【问题标题】:Trying to find a percentage outcome of 3 dice rolls试图找到 3 次掷骰子的百分比结果
【发布时间】:2014-04-28 10:32:58
【问题描述】:

大家好,下面我将粘贴我的代码以找出滚动结果的百分比机会(请参阅我的最后一个结果以获取提醒)

我要做的几乎是掷 3 个骰子,将总数存储在 18 个 texbox 中(每个总数 3,18 个),然后根据滚动次数计算出总数的百分比机会

(一个公平的警告,我的数学有点不对劲,但是与代码混在一起会使情况变得更糟,而且我只是一年级程序员,需要任何帮助:()

Imports System.Math

Public Class Form1
    Dim Numthrow As Integer
    Dim diceValue(18) As Integer
    Dim randomGen = New Random()
    Dim total As Integer
    Dim percenttotal(18) As Integer
    Dim cent As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Numthrow = TextBox1.Text
        For index = 1 To Numthrow
            Dim diceResult1 = randomGen.Next(1, 7)
            Dim diceResult2 = randomGen.Next(1, 7)
            Dim diceResult3 = randomGen.Next(1, 7)
            total = diceResult1 + diceResult2 + diceResult3
            diceValue(total) += 1

            cent = Numthrow * 100 / total
            percenttotal(cent) += 1
        Next

        Label21.Text = percenttotal(3)
        Label22.Text = percenttotal(4)
        Label23.Text = percenttotal(5)
        Label24.Text = percenttotal(6)
        Label25.Text = percenttotal(7)
        Label26.Text = percenttotal(8)
        Label27.Text = percenttotal(9)
        Label28.Text = percenttotal(10)
        Label29.Text = percenttotal(11)
        Label30.Text = percenttotal(12)
        Label31.Text = percenttotal(13)
        Label32.Text = percenttotal(14)
        Label33.Text = percenttotal(15)
        Label34.Text = percenttotal(16)
        Label35.Text = percenttotal(17)
        Label36.Text = percenttotal(18)

        TextBox2.Text = diceValue(3).ToString()
        TextBox3.Text = diceValue(4).ToString()
        TextBox4.Text = diceValue(5).ToString()
        TextBox5.Text = diceValue(6).ToString()
        TextBox6.Text = diceValue(7).ToString()
        TextBox7.Text = diceValue(8).ToString()
        TextBox8.Text = diceValue(9).ToString()
        TextBox9.Text = diceValue(10).ToString()
        TextBox10.Text = diceValue(11).ToString()
        TextBox11.Text = diceValue(12).ToString()
        TextBox12.Text = diceValue(13).ToString()
        TextBox13.Text = diceValue(14).ToString()
        TextBox14.Text = diceValue(15).ToString()
        TextBox15.Text = diceValue(16).ToString()
        TextBox16.Text = diceValue(17).ToString()
        TextBox17.Text = diceValue(18).ToString()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        diceValue(18) = 0
        percenttotal(18) = 0
    End Sub
End Class

【问题讨论】:

  • 是的,所以总结果是 216,我最近将其更改为
  • cent = Numthrow / total * 100 这是 numthrow 可以是 100 / 3 个骰子的总数,可以在 3,18 之间然后乘以 100 得到概率

标签: .net vb.net random probability dice


【解决方案1】:

最好将滚动值存储在 List(of Integer) 中。然后将更容易跟踪数字和总和。

如果您只对三轮后的概率感兴趣,那么您可以硬编码这些值 - 计算这实际上并不容易,因为等式如下所示:

所以使用值listed here

Private _diceRollValues As New List(Of Integer)
Private _rnd As New Random

Private Sub RollClick_Click(sender As Object, e As EventArgs) Handles Button8.Click
    Dim thisRoll As Integer = _rnd.Next(1, 7)
    _diceRollValues.Add(thisRoll)
    Debug.WriteLine("You just rolled: " + thisRoll.ToString)
    Debug.WriteLine("The sum of your " + _diceRollValues.Count.ToString + " rolls is " + _diceRollValues.Sum().ToString)
    If _diceRollValues.Count = 3 Then
        Debug.WriteLine("The probability of this was: " + FindProbabilityFromThreeDice(_diceRollValues.Sum).ToString)
    End If
End Sub

Private Function FindProbabilityFromThreeDice(sum As Integer) As Double
    Select Case sum
        Case 3 : Return 1 / 216
        Case 4 : Return 3 / 216
        Case 5 : Return 6 / 216
        Case 6 : Return 10 / 216
        Case 7 : Return 15 / 216
        Case 8 : Return 21 / 216
        Case 9 : Return 25 / 216
        Case 10 : Return 27 / 216
        Case 11 : Return 27 / 216
        Case 12 : Return 25 / 216
        Case 13 : Return 21 / 216
        Case 14 : Return 15 / 216
        Case 15 : Return 10 / 216
        Case 16 : Return 6 / 216
        Case 17 : Return 3 / 216
        Case 18 : Return 1 / 216
        Case Else
            Throw New Exception("Invalid sum for three rolls of the dice")
    End Select
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2012-11-27
    • 2018-10-19
    • 1970-01-01
    相关资源
    最近更新 更多