[RFC] RandomShuffle
[RFC] RandomShuffle
- Subject: [RFC] RandomShuffle
- From: Arthur J Knapp <email@hidden>
- Date: Wed, 10 Oct 2001 12:22:10 -0400
In a previous posting, I submitted a handler, (NewOrder), that
purported to randomize a list of items, while using only a single
osax call.
Alert reader Timothy Bates discovered that the algorithm was
quite biased, and would "chunk" it's output. The following
handler seems better:
set the_list to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
RandomShuffle(the_list)
the_list
on RandomShuffle(a)
set r to random number -- one osax call
set o to 314159 -- modifies r for each item
set len to a's length
repeat with x from 1 to len
set y to rndUp(r * len) -- an item index
set r to (r * o) mod 1 -- modify r, save decimal part
-- log r -- uncomment to look at "random-ness" of r
set temp to a's item x
set a's item x to a's item y
set a's item y to temp
end repeat
end RandomShuffle
on rndUp(n) -- from Nigel Garvey's aRounderRound
tell n div 1
if it < n then return it + 1
it
end tell
end rndUp
Please let me know if anyone encounters problems using this
handler.
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.zavatone.com/