AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

randomizing elements in array

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
flux



Joined: 23 Aug 2009
Posts: 6

PostPosted: Fri Mar 05, 2010 10:23 am    Post subject: randomizing elements in array Reply with quote

How do I randomize elements in huge arrays without creating duplicates or abusing the processor?

thanks,
flux
Back to top
View user's profile Send private message
Neverlevel



Joined: 28 Jan 2008
Posts: 138
Location: KC

PostPosted: Fri Mar 05, 2010 11:46 am    Post subject: Reply with quote

not sure but probabably index + random - random = newindex should work?
_________________
The early bird gets the worm but the second rat gets the cheese!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Fri Mar 05, 2010 12:21 pm    Post subject: Reply with quote

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
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
flux



Joined: 23 Aug 2009
Posts: 6

PostPosted: Sat Mar 06, 2010 12:23 am    Post subject: Reply with quote

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
}
Back to top
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Sat Mar 06, 2010 12:49 am    Post subject: Reply with quote

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?
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
entropic



Joined: 21 Dec 2008
Posts: 181

PostPosted: Sat Mar 06, 2010 1:31 am    Post subject: Reply with quote

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/viewtopic.php?p=277176&highlight=msgbox+shuffle#277176
Back to top
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Sat Mar 06, 2010 2:02 am    Post subject: Reply with quote

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 Laughing Wink.
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group