MACSCRPT Archives

April 2008

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Nigel Garvey <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Tue, 15 Apr 2008 19:56:09 +0100
Content-Type:
text/plain
Parts/Attachments:
text/plain (41 lines)
"Mark J. Reed" wrote on Mon, 14 Apr 2008 09:34:11 -0400:

>On Mon, Apr 14, 2008 at 8:35 AM, Mark J. Reed <[log in to unmask]> wrote:
>> Easy to see with a small concrete example: the list {1,2,3} has 3!=6
>>  possible permutations, but your routine makes one of 3^3=21 different
>
>... well, 27, actually, but you get the idea. :)
>
>Here are the actual numbers:
>
>Sequence: Probability
>{1, 3, 2}: 5/27
>{2, 1, 3}: 5/27
>{2, 3, 1}: 5/27
>{1, 2, 3}: 4/27
>{3, 1, 2}: 4/27
>{3, 2, 1}: 4/27

OK. I'm convinced. I don't know how you arrived at these probabilities
and the explanation doesn't hang together in my head, but an intensive
test shows that the 5/27 results do tend to come up slightly more often
with my script than do the 4/27s; whereas with your script, the spread is
more even. 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


NG

ATOM RSS1 RSS2