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:
Mon, 14 Apr 2008 12:28:19 +0100
Content-Type:
text/plain
Parts/Attachments:
text/plain (41 lines)
"Mark J. Reed" wrote on Sun, 13 Apr 2008 18:06:11 -0400:

>My code is a complete solution. No need to change it, other than to
>correct my syntax errors.  Here's the corrected version:
>
>on shuffle(aList)
>	set listSize to count aList
>	repeat with i from listSize to 2 by -1
>		set j to random number from 1 to i
>		if j is not i then
>			tell aList to set {item i, item j} to {item j, item i}
>		end if
>	end repeat
>end shuffle

Hi, Mark.

My own attempt at a list randomiser looks like this:

  on shuffle(aList)
    script o
      property l : aList
    end script
    
    set lengthMinus1 to (count aList) - 1
    repeat with i from 1 to lengthMinus1 + 1
      set j to (random number lengthMinus1) + 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

Apart from the fact that it uses AppleScript performance logic
(referenced list variable, different random number syntax, not bothering
to see if j = i, not setting-by-list), are there any randomness
implications in the fact that my item i can be exchanged with any item in
the list, whereas yours is only exchanged with items that come in front of it?

NG

ATOM RSS1 RSS2