Sign in to follow this  
Followers 0
cypher175

Randomly Mix Up an Array..??

5 posts in this topic

Is there any Function or way to Mix Up an Array once it has been made..??

So say I have an Array like this:

Dim $Grid[5]
$Grid[0]="A"
$Grid[1]="B"
$Grid[2]="C"
$Grid[3]="D"
$Grid[4]="E"
_ArrayDisplay($Grid)

Now I want to Mix it up - Randomly Rearrange it..

So when I do another _ArrayDisplay($Grid) it will display the array all randomly Mixed up like..

$Grid[0]="B"
$Grid[1]="D"
$Grid[2]="E"
$Grid[3]="A"
$Grid[4]="C"

Is there anyway or function that can do this at all..??

Share this post


Link to post
Share on other sites



You could do a version of a sorting algorithm, but instead of putting things in order, put things randomly.

; Untested code, may still need to be debugged.  Only provided to illustrate an idea.

Global $I, $Tmp, $A
Global $Cards[52] = ['AS', '2S', '3S', ... 'QC', 'KC']  ;  I am not going to type out all 52 elements for an example.

For $I = 0 To 51 ; Size of array
    ; Find a location to swap the current element with.
    $A = Random(0, 51, 1) 
    ; Do not bother to swap if the two locations are the same
    If $A <> $I Then 
        ; Perform actual swap
        $Tmp = $Cards[$A]
        $Cards[$A] = $Cards[$I]
        $Cards[$I] = $Tmp
    Endif
Next
; Ok, so now things are scrambled.

I hope this helps.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

You sure there's no other simpler way to randomly mixup an array once its already been made..??

Share this post


Link to post
Share on other sites

_ArrayPermute()

look at that function in the helpfile.

Share this post


Link to post
Share on other sites

That looks pretty simple to me and, like Nutster pointed out, it may need some fine tuning.

Why can't you just create the array and then randomly call the elements? That's almost as good. I use that when randomizing large (8k to 40k row) databases.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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