哦哦,差不多就是這種意思~
這樣從資料數 0 到全部加滿,速度會從快到慢
我也來貼一下我的寫法~(不過我的是 VB6 ...),這樣就顛倒過來是從慢到快
不知哪種速度比較快呢?
譬如 25 RndSeedS 是 "/1/2/3/4/5......./25/"
第一個亂數位置(RndN) 譬如 4 就用 UserInstr 找到第 4 個 斜線的位置,結果取出來就是 "4"
第二個亂數位置譬如 5 但是取出來就是 6 了因為 RndSeedS 經過上一次取出已經變成 "/1/2/3/5/6/7..../25/" (4 不見了)
最後 RndSeedS 就剩下例如 "/19/" 亂數位置肯定是 1 就取到 19
這時 RndSeedS 就變成 "/" 取光光了~
底下需要 3 個元件:
TextBox 名稱 TB1
ListBox 名稱 LB1
CommandButton 名稱 CMD_Proc
複製程式
Option Explicit
Dim RndSeedS As String
Private Sub CMD_Proc_Click()
Dim RndN As Long, Ct As Long, i As Long, k1 As Long, k2 As Long
Dim tempS As String
Dim LastTimer As Double
LastTimer = Timer
'------- Ini. Var. & Obj. -------
Ct = Val(TB1.Text)
If Ct <= 0 Then Exit Sub
LB1.Clear
'------- Ini. Randomized Seed --------
RndSeedS = "/"
For i = 1 To Ct
RndSeedS = RndSeedS & i & "/"
Next i
'------ Start Proc. > Select Rnd Num In RndSeedS Then Delete -------
Do
RndN = Fix((Ct) * Rnd + 1) 'Find Pos. of Slash
k1 = UserInstr(RndN) '/x/ < 1st slash
k2 = InStr(k1 + 1, RndSeedS, "/") '/x/ < 2nd slash
tempS = Mid(RndSeedS, k1 + 1, k2 - k1 - 1)
RndSeedS = Left(RndSeedS, k1) & Right(RndSeedS, Len(RndSeedS) - k2)
LB1.AddItem tempS
Ct = Ct - 1
Loop While Ct <> 0
'-------- Show The Time ------
LastTimer = Timer - LastTimer
'Label_C(1).Caption = "Time : " & Format(LastTimer, "0.0000") & " s"
End Sub
Public Function UserInstr(ByVal Ti As Long) As Long 'Find String (Instr) In RndSeedS Continuously
Dim m As Long
m = 0
Do
Ti = Ti - 1
m = InStr(m + 1, RndSeedS, "/")
If m = 0 Then UserInstr = Len(RndSeedS): Exit Function
If Ti = 0 Then UserInstr = m: Exit Function
Loop
End Function
我也來附上我的小程式,也來玩玩看吧,我做了一個 Scan 功能(好像有點無聊..):