Sign in to follow this  
Followers 0
Dieuz

Array_Random

8 posts in this topic

Hey guys,

I am looking for an Autoit function that would do the same thing as the PHP function array_random (http://php.net/manual/en/function.array-rand.php)

I want to pick one or more random entries out of an array without repetition so it should not pick the same entry twice.

Wich autoit function should I use?

I could do this thing in multiple step but I was wondering if there is already one short functionj out there that would help me do that.

Thanks!

Share this post


Link to post
Share on other sites

I think you will have to write the function, but it's not difficult. One way is to randomly select an element and then delete it from the array. Repeat the process until you get to last remaining element.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I have just written this function. It works well! Thanks!

;Select 6 random numbers

            Local $NumberArray[26] = [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
        Local $ResultArray[6]

        For $x = 0 to 5

                $i = Random(1,UBound($NumberArray) - 1, 1)
                _ArrayPush($ResultArray, $NumberArray[$i])
                _ArrayDelete($NumberArray, $NumberArray[$i])

        Next

        _ArrayDisplay($ResultArray)
Edited by Dieuz

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This UDF might help. It puts an array in random order. If you then redim the array to the amount of entires you want, you would have what you need I think.

You could also use that udf and rewrite it to stop at the desired amount of entries, which would be faster.

edit: I see you made something already. I'll have a look.

edit2:

Your script will work ok, but it will be noticably slow when used allot, or on large arrays, due to the redim in _ArrayDelete. (_ArrayPush, might slow it down slightly aswell)

This should be allot faster if performance is important.:

;takes an array and requested amount of indices to return and returns them in an array.
Func _ArrayGetRandomIndices($array,$amount)
    Local $random, $temp, $UBound = UBound($array)
    If Not IsArray($array) Then Return SetError(1) ;$array is not an array
    If $amount > $UBound Then Return SetError(2) ;array is smaller than requested amount of returned indices
    For $i = 0 To $amount-1
    $random = Random(0,$UBound-1,1)
    $temp = $array[$i]
    $array[$i] = $array[$random]
    $array[$random] = $temp
    Next
    ReDim $array[$amount]
    Return $array
EndFunc
Edited by Tvern

Share this post


Link to post
Share on other sites

Func _Array_Random_Shuffle($av_array, $i_lbound = 0)
    Local $i_ubound = UBound($av_array) - 1
    Local $icc, $s_temp, $i_random

    For $icc = $i_lbound To $i_ubound
        $s_temp = $av_array[$icc]
        $i_random = Random($i_lbound, $i_ubound, 1)
        $av_array[$icc] = $av_array[$i_random]
        $av_array[$i_random] = $s_temp
    Next

    Return $av_array
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Well I was beaten to it by SmOke_N, but I might as well post what I wrote. :mellow:

#include <Array.au3>

Local $DataArray = StringSplit("abcdefghijklmnopqrstuvwxyz", ""), $ResultArray[6]

_selectRandomElements($ResultArray)
_ArrayDisplay($ResultArray)

Func _selectRandomElements(ByRef $array)
    Local $x, $Remaining = 26
    For $i = 0 To UBound($ResultArray) -1
        $x = Random(1, $Remaining, 1)
        $array[$i] = $DataArray[$x]
        _ArrayDelete($DataArray, $x)
        $Remaining -= 1
    Next
EndFunc

Which method you choose will probably depend on circumstance: Shuffling 26 elements, when you only need to randomly select 6 elements, may take longer.

Edited by czardas

Share this post


Link to post
Share on other sites

Well I was beaten to it by SmOke_N, but I might as well post what I wrote. :mellow:

#include <Array.au3>

Local $DataArray = StringSplit("abcdefghijklmnopqrstuvwxyz", ""), $ResultArray[6]

_selectRandomElements($ResultArray)
_ArrayDisplay($ResultArray)

Func _selectRandomElements(ByRef $array)
    Local $x, $Remaining = 26
    For $i = 0 To UBound($ResultArray) -1
        $x = Random(1, $Remaining, 1)
        $array[$i] = $DataArray[$x]
        _ArrayDelete($DataArray, $x)
        $Remaining -= 1
    Next
EndFunc

Which method you choose will probably depend on circumstance: Shuffling 26 elements, when you only need to randomly select 6 elements, may take longer.

Using any function that ReDims your array every loop will most assuredly be slower than most others.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Using any function that ReDims your array every loop will most assuredly be slower than most others.

I wasn't sure about it. Thanks for correcting me. :mellow: Edited by czardas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0