At 5:49 PM -0400 9/18/00, Robert Wingate wrote:
>Irv wrote:
>
>>  Or, a little faster algorithm:
>
>[snip]
>
>Nice!
>

Actually, now I typically do it by generating the list in order and 
picking each element at random:

global gRandomList

on generateRandomList, howManyElements
   gRandomList = []
   repeat with i = 1 to howManyElements
     add(gRandomList, i)
   end repeat
end

on getNextRandom
   nItems = count(gRandomList)
   randomIndex = random(nItems)
   randomNumber = gRandomList[randomIndex]
   deleteAt(gRandomList, randomIndex)
   return randomNumber
end

But then again, I would wrap it up into a nice object (untested):

property pRandomList
property pHowManyElements

on new me, howManyElements
   pHowManyElements = howManyElements
   me.mGenerateRandomList()
   return me
end

on mGenerateRandomList me
   pRandomList = []
   repeat with i = 1 to pHowManyElements
     add(gRandomList, i)
   end repeat
end

on mGetNextRandom me
   nItems = count(gRandomList)
   if nItems = 0 then
     memGenerateList()  -- regenerate the list
     nItems = pHowManyElements
   end if
   randomIndex = random(nItems)
   randomNumber = pRandomList[randomIndex]
   deleteAt(gRandomList, randomIndex)
   return randomNumber
end

Irv
-- 
Lingo / Director / Shockwave development for all occasions
            (We even do weddings and Bar Mitzvahs!)
   See our kid's CD-Rom's at:  http://www.furrypants.com

[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to