一个产生随机数的程序
如何产生一系列0到k之间的随机数,并保证这k+1个整数都能取到??
算法复杂度要尽可能的低!!谢谢!!
[解决办法]
dim arr(100) as integer
dim k as integer
for k=0 to 100
rndnum:
randomize
i=rnd*100
for n=lbound(arr) to ubound(arr)
if arr(n)=i then
goto rndnum
end if
next n
next k
[解决办法]
Option Explicit
Private Sub Form_Load()
Const Number = 200
Dim N(Number - 1) As Integer
Dim iLoop As Integer
Dim iA As Integer, iB As Integer, iSwap As Integer
For iLoop = 0 To Number - 1
N(iLoop) = iLoop Mod 100 + 1
Next iLoop
Randomize Timer
For iLoop = 0 To 9999
iA = Int(Rnd * Number)
iB = Int(Rnd * Number)
iSwap = N(iA)
N(iA) = N(iB)
N(iB) = iSwap
Next iLoop
For iLoop = 0 To Number - 1
Debug.Print N(iLoop);
Next iLoop
End
End Sub