What is the quickest way to randomize a list? (was Re: Long text file manipulation--BIGGER QUESTION)
What is the quickest way to randomize a list? (was Re: Long text file manipulation--BIGGER QUESTION)
- Subject: What is the quickest way to randomize a list? (was Re: Long text file manipulation--BIGGER QUESTION)
- From: Timothy Bates <email@hidden>
- Date: Sat, 07 Apr 2001 13:46:34 +1000
hi all,
I have some handlers for randomizing lists. Up till seeing Chris's ever so
clever script, I was making a list of random numbers and then using Akua to
synchronise my originalList with this random list (this transfers the
entropy of the random list into the original list).
Now I see Chris's handler (copied below). How clever: grab a random list
item from those not yet grabbed, and place it at the end of the list! In
place, and very quick.
Can anyone improve on this for speed?
Bench mark:using the script pasted at the bottom of this email, the Nebel
unsort took 2/3 the time, and was robust for large lists (whereas a large
list caused Akua to throw a "patience is virtuous" dialog and thrash a lot.
Mark 1 up for Native AppleScript!
(Of course I benchmarked the same thing in php at 1/10th the time on a DP
Dell server running apache and php4 (still can't get php running under X on
my TiBk) and in LabVIEW on the the TiBk at <1/100th of the time ;-( )
So LabVIEW remains the reigning user-programable champion! <www.ni.com/mac>
tim
6/04/2001 7:57 AM, "Chris Nebel" <email@hidden> wrote:
>
repeat with i from length of pin_list to 1 by -1
>
set current_item to (random number from 1 to i)
>
do_stuff(item current of pin_list)
>
tell pin_list -- exchange item current_item with item i
>
set temp to item i
>
set item i to item current_item
>
set item current_item to temp
>
end tell
>
end repeat
set testList to makeTestList(300)
beep
set t1 to current date
tim(testList)
set t2 to current date
Nebel(testList)
set t3 to current date
return "tim took: " & t2 - t1 & " seconds. Nebel took: " & t3 - t2 &
"seconds"
to tim(originalList)
set randList to {}
repeat with n from 1 to count originalList
set end of randList to random number from 1 to 100000000
end repeat
order list randList synchronizing {originalList}
return originalList
end tim
to Nebel(originalList)
repeat with i from length of originalList to 1 by -1
set current_item to (random number from 1 to i)
tell originalList -- exchange item current_item with item i
set temp to item i
set item i to item current_item
set item current_item to temp
end tell
end repeat
return originalList
end Nebel
to makeTestList(tLength)
set testList to {}
repeat with n from 1 to tLength
set end of testList to random number from 1 to 100000000
end repeat
return testList
end makeTestList