# Re: 交换法

Posted by Knight on November 10, 1998 at 10:59:14:

In Reply to: VB有无 "产生不重复乱数" 之功能函数? posted by 小Sam on October 28, 1998 at 10:24:22:

: 请问各位传统Basic老手或新新人类VB使用者,
: VB有无 "产生不重复乱数" 之功能函数?
: (意指不用任何技巧 ,而是一个函数所提供的功能?)
: 若有 ,是否会因乱数范围大小而影响产生速度?
: 如:1~10000000中取10000组不重复的数字 ,
: 取到后来会不会越取越慢?
: 若无 ,
: 有无较简单方法而又不会造成越取越慢?

: Thanks...小Sam
以下是以交换法达成不重覆乱数
请大家指教
10000000 组数字在p-133约5 min
Option Explicit
Const Random_Num = 10000000
Dim NUM(Random_Num) As Long
End Sub
Private Sub Form_Load()
Dim Change_Num1&, Change_Num2&, Change_Buf&, Count_Num&
Randomize
For Count_Num = 0 To Random_Num
NUM(Count_Num) = Count_Num
Next
For Count_Num = 0 To Random_Num
Change_Num1 = Int(Rnd * Random_Num) + 1
Change_Num2 = Int(Rnd * Random_Num) + 1
Change_Buf = NUM(Change_Num1)
NUM(Change_Num1) = NUM(Change_Num2)
NUM(Change_Num2) = Change_Buf
Next
End Sub

Follow Ups: