很简单的,就是从1到20中,随机抽取10个互不相同的数出来,就像一些彩票软件那样。我冥思苦想一夜不得要领。请大家帮忙,多谢了!

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim a(20)
        Dim i As Integer
        Dim x As Integer
        Dim j As Integer
        
        For i = 0 To 19
            a(i) = i
        Next i
        
        For i = 19 To 10 Step -1
            Randomize
            x = Int((i + 1) * Rnd)
            Debug.Print a(x) + 1,
            For j = x To i - 1
                a(j) = a(j + 1)
            Next j
        Next i
        
        Debug.Print
    End Sub
      

  2.   

    Debug里面输出的就是随即生成的数字
      

  3.   

    TechnoFantasy(www.applevb.com)
    这位仁兄的数组为什么空个a(20)不用啊?
    我想一定不是VB科班出身的。程序基本上是对的,取10至19不重复的数。
      

  4.   

    科班做法 :-)Option Explicit
    Option Base 0Private Sub Command1_Click()
        Dim a(19)
        Dim i As Integer
        Dim x As Integer
        Dim j As Integer
        
        For i = 0 To 19
            a(i) = i
        Next i
        
        For i = 19 To 10 Step -1
            Randomize
            x = Int((i + 1) * Rnd)
            Debug.Print a(x) + 1,
            For j = x To i - 1
                a(j) = a(j + 1)
            Next j
        Next i
        
        Debug.Print
    End Sub
      

  5.   

    dim a as new collection
    dim i as integer
    dim b as integer
    for i = 1 to 20
      a.add i
    next
    for i=1 to 10
      randomize
      b=int(rnd()*a.count)+1
      debug.print a(b)
      a.delete b
    next
      

  6.   

    Dim a(10)
        For i = 1 To 10
    xx:
            Randomize
            a(i) = Int(Rnd() * 20 + 1)
            For j = 1 To i - 1
                If a(j) = a(i) Then GoTo xx
            Next
            Debug.Print a(i)
            
        Next
      

  7.   

    用集合要简单一些:Private Sub Form_Load()
    Dim temp As New Collection
    Dim a(9) As String, num As Integer
    For i = 1 To 20
    temp.Add i
    Next
    Randomize
    For i = 0 To 9
    num = Int(Rnd * temp.Count + 1)
    a(i) = temp(num)
    temp.Remove num
    Next
    Set temp = Nothing
    MsgBox Join(a(), ","), 64, "10 random number between 1 to 20"
    End Sub