标题: 生成100个不重复的随机数 [打印本页] 作者: 盗到稻 时间: 2008-8-2 23:45 标题: 生成100个不重复的随机数 Private Sub Command5_Click()
Dim rn() As String
rn = Split(fncRndNum(1, 100, 100), ",")
Text1 = Join(rn)
End Sub
Private Function fncRndNum(llMinNum As Long, llMaxNum As Long, Num As Long) As String
Dim lsTemp As String
Dim lsNum As String
Do While Num > 0
lsNum = Int(Rnd * (llMaxNum - llMinNum + 1)) + llMinNum
If InStr(lsTemp, lsNum) = 0 Then
lsTemp = IIf(lsTemp = "", "", lsTemp & ",") & lsNum
Num = Num - 1
End If
Loop
fncRndNum = lsTemp
End Function
[ 本帖最后由 t小宝 于 2008-8-3 08:41 编辑 ]作者: ui 时间: 2008-8-3 10:20
在1-100随机,也就是1-100的数都会全用到,对吗作者: 盗到稻 时间: 2008-8-3 17:32
谢谢各位,这是绝简单速度的不重复随机数。先把1到100顺序存入a[]数组中,随机生成一个1到100之间的整数b,交换a[1]与a;再随机生成一个2到100之间的整数b,交换a[2]与a...最后随机生成一个1到100之间的整数b,交换a[30]与a,a[]数组前100个数即所求。
用VB编程如下:
Private Sub Form_Load()
Dim i, j, a(100) As Integer
For i = 1 To 100
a(i) = Str(i)
Next
Text1 = ""
For i = 1 To 100
j = (100 - i) * Rnd + i
a(0) = a(j)
a(j) = a(i)
a(i) = a(0)
Text1 = Text1 & Chr(32) & a(i)
Next
End Sub