Jump to content

_ArrayShuffle UDF


quick_sliver007
 Share

Recommended Posts

Try it out.

;===============================================================================
;
; Function Name:    _ArrayShuffle()
; Description:    Shuffles the elements in an array.
; Parameter(s):  $av_array - The array to shuffle. 
;                $i_base  - The first element of the array.
;                  
;                  ...
;                 
; Requirement(s):   None.
; Return Value(s):  none.
; Author(s):        Quick_sliver007.
; Note(s):        None.
;
;===============================================================================
Func _ArrayShuffle(ByRef $av_array, $i_base = 0)
    Local $max_index = UBound($av_array) -1
    Local $rand
    Local $temp
    Local $x
    For $x = $i_base To $max_index
        $rand = Random($i_base,$max_index)
        $temp = $av_array[$x]
        $av_array[$x] = $av_array[$rand]
        $av_array[$rand] = $temp
    Next
EndFunc
;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include<array.Au3>
Dim $array[10]
For $i = 0 to 9
    $array[$i] =$i
Next
_ArrayDisplay($array,"Before shuffle")
_ArrayShuffle($array)
_ArrayDisplay($array,"After shuffle")

I hope you like it.

.

Link to comment
Share on other sites

I use the RAND func and _ArrayDelete as I go along so I don't repeat.

For $i = $iArrayTotal To 1 Step - 1
    $iRand = Random(1, $i, 1)

    // do stuff here to array element

    _ArrayDelete($aSource, $iRand)
Next

Not sure which would be more efficient (most likely yours is) ;) but then again I wrote mine like 6 months ago.

I like your implementation better tho...

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Link to comment
Share on other sites

Damn...took me like 2 minutes to integrate your shuffle function code into my Shuffler project. ;)

Like I said before, I'm not sure which is faster but my gut instinct sez yours is...I'm too lazy to do time tests :P

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...