Subject: | |
From: | |
Reply To: | |
Date: | Wed, 16 Apr 2008 11:23:06 -0700 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
>>Nigel>> What's more, adapting my code to your algorithm makes it even
faster. :)
> on shuffle(aList)
> script o
> property l : aList
> end script
>
> repeat with i from (count aList) to 2 by -1
> set j to (random number (i - 1)) + 1
> set v to item i of o's l
> set item i of o's l to item j of o's l
> set item j of o's l to v
> end repeat
> end shuffle
Thanks Nigel and Mark, this thread has helped me speed and improve a little
something I've been working on, so here's a practical example tested against
Nigel's version that's a bit faster.
------------
property listIndex : {}
set aDeck to {}
repeat with s in {" of H", " of C", " of S", " of D"}
repeat with x from 1 to 13
if x = 1 then
set c to "A"
else if x = 11 then
set c to "J"
else if x = 12 then
set c to "Q"
else if x = 12 then
set c to "Q"
else if x = 13 then
set c to "K"
else
set c to x as string
end if
set the end of aDeck to c & s
end repeat
end repeat
set listIndex to {}
repeat with x from 1 to count of aDeck
set the end of listIndex to x
end repeat
set startTime to the ticks
ashuffle(aDeck)
set aShuffledDeck to the result
set midTime to the ticks
bshuffle(aDeck)
set bShuffledDeck to the result
set endTime to the ticks
return {midTime - startTime, endTime - midTime, aShuffledDeck,
bShuffledDeck}
on ashuffle(aList)
--my tweak of Nigel's version
script o
property l : aList
end script
repeat with i from (count aList) to 2 by -1
set j to (some item of listIndex)
--doesn't require OSAX call, yet is random
set v to item i of o's l
set item i of o's l to item j of o's l
set item j of o's l to v
end repeat
return o's l
end ashuffle
on bshuffle(aList)
--Nigel's version
script o
property l : aList
end script
repeat with i from (count aList) to 2 by -1
set j to (random number (i - 1)) + 1
set v to item i of o's l
set item i of o's l to item j of o's l
set item j of o's l to v
end repeat
return o's l
end bshuffle
-----
Good luck!
ES
|
|
|