如何使随机数生成器不重复相同的数字超过两次vb?

时间:2016-02-20 20:36:35

标签: vb.net

所以我做了一个非常好的记忆游戏,除了一件事:重复太多了!我想要重复最多2个图像。事情是我尝试的所有代码(如下所示)使每个图片框包含相同的图像而不是具有不同的图像。我按照建议尝试制作一个列表但结果是一样的。我做错了什么?

  Dim generator As New Random
  Dim random As Integer
    Dim picks As New List(Of Integer)
    Dim RndImage(9) As Image

Public Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click

    RndImage(1) = My.Resources.image1
    RndImage(2) = My.Resources.image2
    RndImage(3) = My.Resources.image3
    RndImage(4) = My.Resources.image4
    RndImage(5) = My.Resources.image5
    RndImage(6) = My.Resources.image6
    RndImage(7) = My.Resources.image7
    RndImage(8) = My.Resources.image8
    RndImage(9) = My.Resources.image9

     Do
        random = generator.Next(1, 9)
        If picks.Contains(random) = False Then
            picks.Add(random)
        End If
    Loop Until picks.Count = 18

     PictureBox1.Image = RndImage(random)
    PictureBox2.Image = RndImage(random)
    PictureBox3.Image = RndImage(random)
    PictureBox4.Image = RndImage(random)
    PictureBox5.Image = RndImage(random)
    '...
    PictureBox18.Image = RndImage(random)
End Sub

0 个答案:

没有答案