Visual Basic:老虎机值都返回相同的值

时间:2018-09-24 12:33:23

标签: vb.net random

我试图在Visual Basic Windows窗体创建器中创建一个简单的老虎机。到目前为止,这是我的代码:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim List1 = New String() {"Banana", "Cherry", "Watermelon", "Bell", "Skull", "Apricot"}
        Dim List2 = New String() {"Banana", "Cherry", "Watermelon", "Bell", "Skull", "Apricot"}
        Dim List3 = New String() {"Banana", "Cherry", "Watermelon", "Bell", "Skull", "Apricot"}
        Dim rn1 = New Random()
        Dim rn2 = New Random()
        Dim rn3 = New Random()
        Dim x As Integer = 0
        While x < 30
            Dim Choice = List1(rn1.Next(0, List1.Count))
            Dim Choice2 = List2(rn2.Next(0, List2.Count))
            Dim Choice3 = List3(rn3.Next(0, List3.Count))

            Select Case Choice
                Case "Banana"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Banana
                Case "Cherry"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Cherry
                Case "Watermelon"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Watermelon
                Case "Skull"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Skull
                Case "Apricot"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Apricot
                Case "Bell"
                    Slot1.Image = Fruit_Machine.My.Resources.Resources.Bell
            End Select
            Select Case Choice2
                Case "Banana"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Banana
                Case "Cherry"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Cherry
                Case "Watermelon"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Watermelon
                Case "Skull"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Skull
                Case "Apricot"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Apricot
                Case "Bell"
                    Slot2.Image = Fruit_Machine.My.Resources.Resources.Bell
            End Select
            Select Case Choice3
                Case "Banana"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Banana
                Case "Cherry"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Cherry
                Case "Watermelon"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Watermelon
                Case "Skull"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Skull
                Case "Apricot"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Apricot
                Case "Bell"
                    Slot3.Image = Fruit_Machine.My.Resources.Resources.Bell
            End Select

            x = x + 1
            Application.DoEvents()
        End While
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

这确实起作用:投币机的每个结果都是随机的……在一定程度上。我知道代码过于复杂,但这只是一个测试项目,其他任何人都不会看到。对VB来说是新手,我知道这段代码可能是...混乱。但是,当水果机的结果显示所有3个插槽都具有相同的值时,就会出现主要问题。 I.E。

This displays that the result is all 3 skulls

我需要一个解决方案。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

快速连续实例化三个Random对象会导致它们从系统时钟中获得相同的种子值,因此它们都产生相同的序列。将Random(Int32)构造函数与不同的种子一起使用,以获取三个不同的随机值序列,或者仅使用一个Random对象来生成所有值。来自the documentation(添加了重点):

  

要生成不同的随机数序列,可以使种子值与时间相关,从而对每个新的Random实例生成不同的序列。参数化的Random(Int32)构造函数可以基于当前时间的滴答数获取Int32值,而无参数的Random()构造函数使用系统时钟生成其种子值。 但是,由于时钟具有有限的分辨率,因此使用无参数构造函数连续创建不同的Random对象会创建随机数生成器,该生成器会生成相同的随机数序列。

答案 1 :(得分:2)

我相信这是因为Random的每个实例都使用相同的种子(系统时间),请尝试使用Random的相同实例并在其上调用Next,例如

    Dim myRandom = New Random()

    Dim x As Integer = 0
    While x < 30
        Dim Choice = List1(myRandom.Next(0, List1.Count))
        Dim Choice2 = List2(myRandom.Next(0, List2.Count))
        Dim Choice3 = List3(myRandom.Next(0, List3.Count))