请各位大侠给个思路
老师我的表述不清楚
1.我想从数组c(1),c(2),c(3)........c(a)......c(35)共35个整数中随机取5个不同整数,为保证不同,我不想采用一般的遍历法(相同的再重新取),而是想取一个随机整数a(介于1-35)对应从c(1),c(2),c(3)........c(a)......c(35)拿出c(a),
而原来的数组只余下c(1),c(2),c(3)........c(a-1),c(a+1),............c(35)(共34个数)仍保持原来顺序,
然后取一个随机整数b(介于1-34)对应从c(1),c(2),c(3)........c(a-1),c(a+1),............c(35)中拿出c(b)(在新顺序中第b个),
只余下c(1),c(2),c(3)........c(b-1),c(b+1),............c(35)(共33个数)仍保持原来顺序
以此类推得到数组d(5),其中d(1)=c(a),d(2)=c(b),d(3)=c(c),d(4)=c(d),d(5)=c(e),
2.将以上过程执行n次得到n个数组d(n,5)
综合以上考虑,想到用列表框。由于是个菜鸟,作乱了
老师,谢谢您,请问怎样实现
这样如果能实现,感觉到取随机数比较广泛一些。
[解决办法]
本帖最后由 bcrun 于 2013-07-15 17:19:26 编辑
function GetDifference() as long()
dim a(1 to 35) as long
dim B(4) as long
dim i as long
for i=0 to 4
retry:
n=1+rnd*35
if a(n)=1 then retry 'a(n)=1表示n这个数已经用过了
b(i)=c(n)
a(n)=1
next
getdifference=b
end function
Option Explicit
Private seed As Long
Private Const a As Long = 71
Private Const m As Long = 35
Private Const c As Long = 66
Function Rand() As Long
seed = (seed * a + c) Mod m
Rand = seed
End Function
Sub Main()
Dim i As Long
Randomize
seed = Int(Rnd() * m)
For i = 1 To m
Debug.Print i, Rand + 1
Next
End Sub
Sub Check()
Dim aUsed() As Boolean
Dim r As Long
Dim i As Long
Dim j As Long
For j = 0 To m - 1
seed = j
Debug.Print "验证 seed = " & j
ReDim aUsed(1 To m)
For i = 1 To m
r = Rand + 1
If aUsed(r) Then
Debug.Print "重复: " & r
Else
aUsed(r) = True
End If
Next
Next
End Sub
Dim i As Integer, n As Integer
List1.Clear
For i = 1 To 35
List1.AddItem c(i)
Next i
Randomize
For i = 1 To 5
n = Int(Rnd * List1.ListCount)
d(i) = List1.List(n)
List1.RemoveItem n
Next i