Excel VBA 亂數Rnd
Function Fn1_大樂透電腦選號()
'陣列必需指定常數
Const All_Ball = 49
Const ShowBall = 6
'以下程式碼引用凸槌的木工
Dim i%, num%
Dim a(1 To All_Ball) As Boolean
Dim n(1 To ShowBall) As Integer
Erase a '清除a陣列
For i = 1 To ShowBall
num = Int(All_Ball * Rnd + 1) '取得亂數(可能重覆)
Do While a(num) = True '若陣列a內已加入num則執行下句:
num = Int(All_Ball * Rnd + 1) '重新取得亂數
Loop
a(num) = True '把num加入陣列
n(i) = num
Next i
'
Range("A1:f1") = n
End Function
'陣列必需指定常數
Const All_Ball = 49
Const ShowBall = 6
'以下程式碼引用凸槌的木工
Dim i%, num%
Dim a(1 To All_Ball) As Boolean
Dim n(1 To ShowBall) As Integer
Erase a '清除a陣列
For i = 1 To ShowBall
num = Int(All_Ball * Rnd + 1) '取得亂數(可能重覆)
Do While a(num) = True '若陣列a內已加入num則執行下句:
num = Int(All_Ball * Rnd + 1) '重新取得亂數
Loop
a(num) = True '把num加入陣列
n(i) = num
Next i
'
Range("A1:f1") = n
End Function
留言
張貼留言