VBScript array_rand function

A VBScript equivalent of PHP’s array_rand

Function array_rand(mAry, ByVal num_req)

    If Not isArray( mAry ) Then Exit Function
    If Not isNumeric( num_req ) Then num_req = 1

    Dim rand,i,intCounter,aryCounter,indexes

    intCounter = uBound(mAry)
    aryCounter = num_req -1

    If intCounter < aryCounter Then Exit Function


    ReDim indexes( aryCounter )
    For i = 0 to aryCounter
        Do While true
            rand = Round( Rnd * uBound(mAry) )
            If Not in_array(rand, indexes,true) Then
                indexes(i) = rand
                Exit Do
            End If

    If num_req = 1 Then
        array_rand = indexes(0)
        array_rand = indexes
    End If

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension