最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP函数 随机输出数组中元素Shuffle()

日期:04-23    来源:中国设计秀    作者:cnwebshow.com

<%2Zh中国设计秀
Sub Shuffle (ByRef arrInput)2Zh中国设计秀
    'declare local variables:2Zh中国设计秀
    Dim arrIndices, iSize, x2Zh中国设计秀
    Dim arrOriginal2Zh中国设计秀

    'calculate size of given array:2Zh中国设计秀
    iSize = UBound(arrInput)+12Zh中国设计秀

    'build array of random indices:2Zh中国设计秀
    arrIndices = RandomNoDuplicates(0, iSize-1, iSize)2Zh中国设计秀

    'copy:2Zh中国设计秀
    arrOriginal = CopyArray(arrInput)2Zh中国设计秀

    'shuffle:2Zh中国设计秀
    For x=0 To UBound(arrIndices)2Zh中国设计秀
        arrInput(x) = arrOriginal(arrIndices(x))2Zh中国设计秀
    Next2Zh中国设计秀
End Sub2Zh中国设计秀

Function CopyArray (arr)2Zh中国设计秀
    Dim result(), x2Zh中国设计秀
    ReDim result(UBound(arr))2Zh中国设计秀
    For x=0 To UBound(arr)2Zh中国设计秀
        If IsObject(arr(x)) Then2Zh中国设计秀
            Set result(x) = arr(x)2Zh中国设计秀
        Else2Zh中国设计秀
            result(x) = arr(x)2Zh中国设计秀
        End If2Zh中国设计秀
    Next2Zh中国设计秀
    CopyArray = result2Zh中国设计秀
End Function2Zh中国设计秀

Function RandomNoDuplicates (iMin, iMax, iElements)2Zh中国设计秀
    'this function will return array with "iElements" elements, each of them is random2Zh中国设计秀
    'integer in the range "iMin"-"iMax", no duplicates.2Zh中国设计秀

    'make sure we won't have infinite loop:2Zh中国设计秀
    If (iMax-iMin+1)>iElements Then2Zh中国设计秀
        Exit Function2Zh中国设计秀
    End If2Zh中国设计秀

    'declare local variables:2Zh中国设计秀
    Dim RndArr(), x, curRand2Zh中国设计秀
    Dim iCount, arrValues()2Zh中国设计秀

    'build array of values:2Zh中国设计秀
    Redim arrValues(iMax-iMin)2Zh中国设计秀
    For x=iMin To iMax2Zh中国设计秀
        arrValues(x-iMin) = x2Zh中国设计秀
    Next2Zh中国设计秀

    'initialize array to return:2Zh中国设计秀
    Redim RndArr(iElements-1)2Zh中国设计秀

    'reset:2Zh中国设计秀
    For x=0 To UBound(RndArr)2Zh中国设计秀
        RndArr(x) = iMin-12Zh中国设计秀
    Next2Zh中国设计秀

    'initialize random numbers generator engine:2Zh中国设计秀
    Randomize2Zh中国设计秀
    iCount=02Zh中国设计秀

    'loop until the array is full:2Zh中国设计秀
    Do Until iCount>=iElements2Zh中国设计秀
        'create new random number:2Zh中国设计秀
        curRand = arrValues(CLng((Rnd*(iElements-1))+1)-1)2Zh中国设计秀

        'check if already has duplicate, put it in array if not2Zh中国设计秀
        If Not(InArray(RndArr, curRand)) Then2Zh中国设计秀
            RndArr(iCount)=curRand2Zh中国设计秀
            iCount=iCount+12Zh中国设计秀
        End If2Zh中国设计秀

        'maybe user gave up by now...2Zh中国设计秀
        If Not(Response.IsClientConnected) Then2Zh中国设计秀
            Exit Function2Zh中国设计秀
        End If2Zh中国设计秀
    Loop2Zh中国设计秀

    'assign the array as return value of the function:2Zh中国设计秀
    RandomNoDuplicates = RndArr2Zh中国设计秀
End Function2Zh中国设计秀

Function InArray(arr, val)2Zh中国设计秀
    Dim x2Zh中国设计秀
    InArray=True2Zh中国设计秀
    For x=0 To UBound(arr)2Zh中国设计秀
        If arr(x)=val Then2Zh中国设计秀
            Exit Function2Zh中国设计秀
        End If2Zh中国设计秀
    Next2Zh中国设计秀
    InArray=False2Zh中国设计秀
End Function2Zh中国设计秀

'usage:2Zh中国设计秀
Dim arrTest2Zh中国设计秀
arrTest = Array(5, 8, 10, 15, 2, 30)2Zh中国设计秀
Call Shuffle(arrTest)2Zh中国设计秀
Response.Write(Join(arrTest, "<br />"))2Zh中国设计秀
%>2Zh中国设计秀
 2Zh中国设计秀

本文引用地址:/bc/article_46292.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明