如题,编写一个双色球抽奖器.红球1-33号抽六个,蓝球1-16号抽一个,要求随机抽取,抽取各个球机率一致.

解决方案 »

  1.   

    给你一个简单易行的方法:
    1 在窗体上放三个 ListBox,分别放红球和蓝球号码。其中 List3 的 Sorted 设置为 True。如果你不想显示它们,也可以将其 Visible 设置为 False。2 代码:
    Dim i As Integer, strTmp As StringPrivate Sub Form_Load()
       List1.Clear
       List1.ForeColor = vbRed
       For i = 1 To 33
          List1.AddItem i
       Next i   List2.Clear
       List2.ForeColor = vbBlue
       For i = 1 To 16
          List2.AddItem i
       Next i   Command1.Caption = "开奖"
       Command2.Caption = "准备"
    End SubPrivate Sub Command1_Click()
    Dim n As Integer   List3.Clear
       List3.ForeColor = vbRed
       List3.Sorted = True
       Randomize
       For i = 1 To 6
          n = Int(Rnd() * List1.ListCount)
          If n = List1.ListCount Then n = n - 1
          List3.AddItem Format(List1.List(n), "0#")
          List1.RemoveItem n 
       Next i   n = Int(Rnd() * List2.ListCount)
       If n = List2.ListCount Then n = n - 1   strTmp = "红球:" & vbCrLf
       For i = 1 To 6
          strTmp = strTmp & List3.List(i - 1) & " "
       Next i
       strTmp = strTmp & vbCrLf & "蓝球:" & vbCrLf & List2.List(n)   Label1 = strTmp  
    End SubPrivate Sub Command2_Click()
       Form_Load
    End Sub
      

  2.   

    我觉得主要使用rnd函数就可以了。33之前的数取6次(不能重复)34~49之间的数取3次(不能重复),两次取数组合在一起不就行了。记得使用Randomize。所取结果机率绝对一样。不推荐使用控件,这种简单的运算使用控件会强烈减低程序速度。
      

  3.   

    去掉   List3.Sorted = True, 在设计时属性窗口中设置。
      

  4.   

    为什么程序中不能加List3.Sorted = True呢?
    还有,蓝球还是没有显示出来