试图找到3个骰子卷的百分比结果

时间:2014-04-28 10:32:59

标签: .net vb.net random probability dice

下面的嘿家伙我会粘贴我的代码,以找出推动结果的百分比机会(请参阅我的最后结果提醒)

我想要做的就是滚动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

1 个答案:

答案 0 :(得分:2)

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

如果您只对三次滚动后的概率感兴趣,那么您可以对这些值进行硬编码 - 计算这实际上并不容易,因为等式看起来像这样:

enter image description here

所以使用值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