有一个数组或者数据库,想要随机取几个值,要求不重复,怎么写?
------解决方案--------------------
重新发完成的算了。。
- VBScript code
Function rndint(byval MaxNum,byval MinNum)'asp生成整数之间的随机数函数 dim tmp if MaxNum<MinNum then tmp=MinNum:MinNum=MaxNum:MaxNum=tmp end if Randomize rndint=int(rnd *(MaxNum-MinNum+1)+MinNum) End Function 'arr:数组 'num:要取的个数 function rndarray(arr,num) if num<1 then rndarray="":exit function dim nownum,idxs,values,arrubound nownum=0'记录取了多少个 idxs=""'记录取到的下标,用,分隔开 values=""'记录取到的数组的值,用,分隔开 arrubound=ubound(arr)'数组上限,随机数最大值 while nownum<num rndnum=rndint(0,arrubound) if instr(idxs,","&rndnum&",")=0 then'未随机到这个下标 nownum=nownum+1 if idxs="" then idxs=","&rndnum&"," else idxs=idxs&rndnum&"," end if values=values&","&arr(rndnum) end if wend rndarray=right(values,len(values)-1) end function vs=rndarray(array(1,2,3,4,5,6),5) respons.write vs&"<br/>" vs=rndarray(array(1,2,3,4,5,6),5) respons.write vs&"<br/>" vs=rndarray(array(1,2,3,4,5,6),5) respons.write vs&"<br/>"