<% '随机遍历数组,很巧妙的算法 Dim i Dim a(99), bound bound =UBound(a) For i =0To bound a(i) = i +1 Next Dim x For i = bound To0 Step -1 x =CInt(Rnd*(i+1)) '记得这里 i要加上 1 response.write a(x) &"<br>" '每次都把随机取到的数组内的某个值,用一个循环由下标从低到高依次替换; '这时候,随机取到的值就在当前循环中被去除了,不可能再次被随机到。 For j = x To i-1 a(j) = a(j+1) Next Next %>
<% '随机遍历数组的另一种方法,但是破坏了原数组结构 Dim i, a(99), bound bound =UBound(a) For i =0To bound a(i) = i +1 Next For i = bound To0 Step -1 Dim x, temp x =CInt(Rnd* (i+1)) response.write a(x) &"<br>" '把随机到的那个跟当前循环中下标最大的那个交换 temp = a(x) a(x) = a(i) a(i) = temp Next %>