AutoHotkey Community

It is currently May 27th, 2012, 4:37 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: March 5th, 2010, 11:23 am 
Offline

Joined: August 23rd, 2009, 4:46 am
Posts: 6
How do I randomize elements in huge arrays without creating duplicates or abusing the processor?

thanks,
flux


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2010, 12:46 pm 
Offline

Joined: January 28th, 2008, 9:40 pm
Posts: 140
Location: KC
not sure but probabably index + random - random = newindex should work?

_________________
The early bird gets the worm but the second rat gets the cheese!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2010, 1:21 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
Perhaps my random function can help?
Code:
Loop
   msgbox % "Element# " . A_Index . "`nRandom Element: " . array%A_index% := rnd(0,10,1)

; For more info on random functions:
; http://www.autohotkey.com/forum/viewtopic.php?t=54713
; http://www.autohotkey.com/forum/viewtopic.php?t=54622

rnd(mn,mx,ml){
    Random, num, % mn * ml, % mx * ml
    return num
}


Or to get multiple non repetative random character elements (pheww).

Code:
Loop
   msgbox % "Element# " . A_Index . "`nRandom Element: " . array%A_index% := rndName(48,122,6)

; You can decrease/increase the character length by editing the last number in the function call.
; So rndName(48,122,6) will give 6 characters.
; While rndName(48,122,2) will give 2 characters.
; and so on....

rndName(minNum,maxNum,nLen){
    slot=0
    While (StrLen(chrList) < nLen)
    {
        slot++, prvslot := slot - 1
        Random, chrNum%slot%, % minNum, % maxNum
        curChr := Chr(chrNum%slot%), lstChr := Chr(chrNum%prvslot%)
            While (chrNum%slot% = chrNum%prvslot%)
            {
                Random, chrNum%slot%, % minNum, % maxNum
                curChr := Chr(chrNum%slot%)
            }
            if curChr is alnum
                chrList .= curChr
    }
    return chrList
}


hth

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2010, 1:23 am 
Offline

Joined: August 23rd, 2009, 4:46 am
Posts: 6
Thanks for the help guys!

It turned out what I was actually looking for was shuffling elements in an array. Sorry for not being clear.

It's called the Knuth shuffle. In case someone looked for it for the next NASA launch:

Code:
Loop % array0-1 {
   Random i, A_Index, array0
   n := array%i%, array%i% := array%A_Index%, array%A_Index% := n
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2010, 1:49 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
I really like that!

The only thing that doesn't work is using an array to decide iteration.
Code:
Loop % array0-1
I think theres a simple workaround for this using while with a comparable expression.

I will post if I figure it out..

edit:

So it looks like array0 is going to equal nothing while its being declared as array0-1. I can only get it to run once if I insert a 2 (n>1) into array0.
Code:
array0 := 2
Loop % array0-1 {
   Random i, A_Index, array0
   n := array%i%, array%i% := array%A_Index%, array%A_Index% := n

}
Is this just a code snippet with some other value in array0 or am I missing something else?

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2010, 2:31 am 
Offline

Joined: December 21st, 2008, 7:29 pm
Posts: 181
This works fine for me
Code:
array0 = 2
array1 = a
array2 = b
array3 = c
array4 = d

Msgbox %array1% %array2% %array3% %array4%

Loop % array0-1
{
  Msgbox %A_Index%
   Random i, A_Index, array0
   n := array%i%, array%i% := array%A_Index%, array%A_Index% := n
}

Msgbox %array1% %array2% %array3% %array4%


Edit:
The one above is pretty much Laszlo's implementation from here:
http://www.autohotkey.com/forum/viewtop ... fle#277176


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2010, 3:02 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
entropic wrote:
This works fine for me
Code:
array0 = 2
array1 = a
array2 = b
array3 = c
array4 = d
So I was correct when I said that the array must init with (n > 1).
entropic wrote:
The one above is pretty much Laszlo's implementation
Laszlo declaring single dimensional boxes? Surely you jest :lol: ;).

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rbrtryn and 64 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group