从九个中选择四个随机数而不重复

时间:2014-02-27 10:03:22

标签: arrays if-statement vb6

我想生成9个随机数。然后我会从那9个中得到4个随机数,但是这四个随机条目*不能重复。 有28种可能的结果。随机化工作正常,但我不明白为什么我不能得到复选框来检查。我一直在努力,请帮助

    Dim rn As New Random
    Dim n(9) As Integer
    n(1) = rn.Next(1, 2)
    n(2) = rn.Next(1, 3)
    n(3) = 1
    n(4) = rn.Next(1, 3)
    n(5) = 1
    n(6) = rn.Next(1, 4)
    n(7) = rn.Next(1, 7)
    n(8) = rn.Next(1, 4)
    n(9) = rn.Next(1, 3)

    Dim loopcount As Integer = 4
    Dim l(4), ln, lm As Integer
    ln = 1
    lm = 1

    l(1) = rn.Next(1, 9)
    l(2) = rn.Next(1, 9)
    While l(2) = l(1)
        l(2) = rn.Next(1, 9)
    End While
    l(3) = rn.Next(1, 9)
    While l(3) = l(2) Or l(3) = l(1)
        l(3) = rn.Next(1, 9)
    End While
    l(4) = rn.Next(1, 9)
    While l(4) = l(3) Or l(4) = l(2) Or l(4) = l(1)
        l(3) = rn.Next(1, 9)
    End While

    MsgBox(l(1) & l(2) & l(3) & l(4))
    MsgBox(n(1) & n(2) & n(3) & n(4) & n(5) & n(6) & n(7) & n(8) & n(9))

    While loopcount > 0
        If ln = 1 Then lm = l(1)
        If ln = 2 Then lm = l(2)
        If ln = 3 Then lm = l(3)
        If ln = 4 Then lm = l(4)

        If l(ln) = 1 & n(lm) = 1 Then 1.CheckState = 1
        If l(ln) = 1 & n(lm) = 2 Then 2.CheckState = 1
        If l(ln) = 2 & n(lm) = 1 Then 3.CheckState = 1
        If l(ln) = 2 & n(lm) = 2 Then 4.CheckState = 1
        If l(ln) = 2 & n(lm) = 3 Then 5.CheckState = 1
        If l(ln) = 3 & n(lm) = 1 Then 6.CheckState = 1
        If l(ln) = 4 & n(lm) = 1 Then 7.CheckState = 1
        If l(ln) = 4 & n(lm) = 2 Then 8.CheckState = 1
        If l(ln) = 4 & n(lm) = 3 Then 9.CheckState = 1
        If l(ln) = 5 & n(lm) = 1 Then 10.CheckState = 1
        If l(ln) = 6 & n(lm) = 1 Then 11.CheckState = 1
        If l(ln) = 6 & n(lm) = 2 Then 12.CheckState = 1
        If l(ln) = 6 & n(lm) = 3 Then 13.CheckState = 1
        If l(ln) = 6 & n(lm) = 4 Then 14.CheckState = 1
        If l(ln) = 7 & n(lm) = 1 Then 15.CheckState = 1
        If l(ln) = 7 & n(lm) = 2 Then 16.CheckState = 1
        If l(ln) = 7 & n(lm) = 3 Then 17.CheckState = 1
        If l(ln) = 7 & n(lm) = 4 Then 18.CheckState = 1
        If l(ln) = 7 & n(lm) = 5 Then 19.CheckState = 1
        If l(ln) = 7 & n(lm) = 6 Then 20.CheckState = 1
        If l(ln) = 7 & n(lm) = 7 Then 21.CheckState = 1
        If l(ln) = 8 & n(lm) = 1 Then 22.CheckState = 1
        If l(ln) = 8 & n(lm) = 2 Then 23.CheckState = 1
        If l(ln) = 8 & n(lm) = 3 Then 24.CheckState = 1
        If l(ln) = 8 & n(lm) = 4 Then 25.CheckState = 1
        If l(ln) = 9 & n(lm) = 1 Then 26.CheckState = 1
        If l(ln) = 9 & n(lm) = 2 Then 27.CheckState = 1
        If l(ln) = 9 & n(lm) = 3 Then 28.CheckState = 1

        ln = ln + 1
        loopcount = loopcount - 1
    End While

    Call Generate()

1 个答案:

答案 0 :(得分:4)

我认为您需要重命名复选框或使用方括号:

[1].CheckState = 1 'for example.

此外,您使用的&应为And ...

If l(ln) = 1 & n(lm) = 1 Then 1.CheckState = 1

......应该......

If l(ln) = 1 And n(lm) = 1 Then [1].CheckState = 1

虽然这可以缩短为......

[1].CheckState = 1 And (l(ln) = 1) And (n(lm) = 1)