asp对数组随机取值不重复

  asp对数组随机取值,不重复取同一个数组里面的值。源代码如下

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)
msgbox vs
vs=rndarray(array(1,2,3,4,5,6),5)
msgbox vs
vs=rndarray(array(1,2,3,4,5,6),5)
msgbox vs

 


原创文章,转载请注明出处:asp对数组随机取值不重复

评论(0)Web开发网
阅读(99)喜欢(0)Asp/VBScript