Subject: | |
From: | |
Reply To: | |
Date: | Fri, 18 Apr 2008 14:28:09 +0100 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
"Stockly, Ed" wrote on Wed, 16 Apr 2008 12:55:11 -0700:
>set listIndex to {}
>repeat with x from 1 to count of fullDeck
> set the end of listIndex to x
>end repeat
>on ashuffle(aList)
> script o
> property l : aList
> end script
> repeat with i from (count aList) to 2 by -1
> set j to (some item of listIndex)
> 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
Hi, Ed. Although this is very fast, it has the same problem as my first
script in this thread: namely that 'j' can index any item in the list. As
Mark's been patiently explaining to me both on and off list, this makes
some shuffle results (marginally) more likely than others. To make all
possible results equally likely, j has to be restricted to a random
number from 1 to i.
on ashuffle(aList)
script o
property l : aList
end script
repeat with i from (count aList) to 2 by -1
set j to (some item of (items 1 thru i of my listIndex))
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 the machine I'm using at the moment, this appears to be slightly
faster even than your version! (Possibly because I've used 'my' to
reference the 'listIndex' property.)
>set listIndex to {}
>repeat with x from 1 to count of fullDeck
> set the end of listIndex to x
>end repeat
>copy fullDeck to aDeck
>copy fullDeck to bDeck
>set startTime to the ticks
>repeat 10 times
> set aDeck to ashuffle(aDeck)
>end repeat
To be fair when comparing the timings of a handler that uses an index
list and one that doesn't, you should include the time taken to build the
list! ;)
copy fullDeck to aDeck
copy fullDeck to bDeck
set startTime to the ticks
set listIndex to {}
repeat with x from 1 to count of fullDeck
set the end of listIndex to x
end repeat
repeat 10 times
set aDeck to ashuffle(aDeck)
end repeat
... though it doesn't seem to make any difference. The 'some item' approach
wins hands down anyway. :)
NG
|
|
|