想用vba來實作雙色球的7位數選擇。并且不重合,怎么樣實作呢?并生成保存到excel里面,謝謝!
uj5u.com熱心網友回復:
請參考:(彩票有風險,投資須謹慎
)Option Explicit
Sub test()
Dim iDic As New Dictionary, w1 As String, ww
Randomize ''隨機初始化
''紅色球1-33任意取6個
Do
w1 = Format$(CInt(Rnd * 33) + 1, "0#")
If Not iDic.Exists(w1) Then iDic(w1) = "0"
If iDic.Count = 6 Then Exit Do
Loop
''藍色球取1個
w1 = Format$(Int(Rnd * 16) + 1, "0#")
''輸出
Debug.Print Join(iDic.Keys, ",") & "--" & w1
iDic.RemoveAll ''清空字典
End Sub
uj5u.com熱心網友回復:
你說的“不重合”是指隨機產生的一注號碼內沒重復號的吧?要保存到 Excel里,這個說得太含糊了……
是指定檔案?還是產生一組號碼后新建檔案?
每次產生多少注號碼?是固定的,還是某范圍內隨機的?還是產生前自己輸入數量?
唉,還是先看看產生一注號碼的吧:
Private Sub Command1_Click()
Dim sOut As String
Dim aBuf() As Long, SUM As Long
Dim i As Long, t As Long
SUM = 32: ReDim aBuf(SUM)
For i = 0 To SUM
aBuf(i) = i + 1
Next
Randomize: sOut = ""
For i = 1 To 6 ' 紅色球
t = SUM * Rnd
sOut = sOut & Right$("0" & aBuf(t), 2) & " "
aBuf(t) = aBuf(SUM)
SUM = SUM - 1
Next
sOut = sOut & Right$("0" & CInt(1 + 15 * 0.98), 2)
MsgBox sOut
End Sub
uj5u.com熱心網友回復:
暈, CInt(1 + 15 * 0.98) 這個0.98忘記改成 Rnd 了。uj5u.com熱心網友回復:
是想有個程式可以把所有的排列組合都可以生成到一個表里面,或者文本里面的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/132484.html
標籤:VBA
