| View previous topic :: View next topic |
| Author |
Message |
flux
Joined: 23 Aug 2009 Posts: 6
|
Posted: Fri Mar 05, 2010 10:23 am Post subject: randomizing elements in array |
|
|
How do I randomize elements in huge arrays without creating duplicates or abusing the processor?
thanks,
flux |
|
| Back to top |
|
 |
Neverlevel
Joined: 28 Jan 2008 Posts: 138 Location: KC
|
Posted: Fri Mar 05, 2010 11:46 am Post subject: |
|
|
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 |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Fri Mar 05, 2010 12:21 pm Post subject: |
|
|
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 |
|
 |
flux
Joined: 23 Aug 2009 Posts: 6
|
Posted: Sat Mar 06, 2010 12:23 am Post subject: |
|
|
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 |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Sat Mar 06, 2010 12:49 am Post subject: |
|
|
I really like that!
The only thing that doesn't work is using an array to decide iteration. 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 |
|
 |
entropic
Joined: 21 Dec 2008 Posts: 181
|
Posted: Sat Mar 06, 2010 1:31 am Post subject: |
|
|
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 |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Sat Mar 06, 2010 2:02 am Post subject: |
|
|
| 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 . _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
|