【备忘】ASP数组随机排序

将数组随机排序,或者说输出N个不重复的随机数,

<%
Randomize
Dim m,n,MyArray(10),Temp
For m = 0 to Ubound(MyArray)
MyArray(m) = m
Next
For n = 0 to Ubound(MyArray)
RndNum = int(rnd*Ubound(MyArray))
Temp = MyArray(n)
MyArray(n) = MyArray(RndNum)
MyArray(RndNum) = Temp
Next
For n = 0 to Ubound(MyArray)
Response.Write MyArray(n) & "<br />"
Next

Response.Write "-------------------<br />"
'下边这种可以少写两行

For m = 0 to Ubound(MyArray)
MyArray(m) = m
Next
For n = 0 to Ubound(MyArray)
RndNum = int(rnd*(Ubound(MyArray)-n+1)+n)
Temp = MyArray(n)
MyArray(n) = MyArray(RndNum)
MyArray(RndNum) = Temp
Response.Write MyArray(n) & "<br />"
Next
%>

原文链接:https://www.wdssmq.com/post/20130902595.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。