are <Proponent@gmx.net> wrote:
+---------------
| What's a good way to randomize a list? When I needed to do this
| recently, I resorted to CONSing each list element with a random
| number, sorting by the random numbers and then stripping them back
| out. This works, but doing a sort when all I need is randomization is
| obviously highly inefficient.
+---------------
I'm not sure what's so "obviously highly inefficient" about a SORT,
since generating the number of good-quality random numbers you will
need for decent randomization is going to be *much* more expensive
than a simple SORT at the end. But if you don't like your method,
then try this:
(coerce (random-shuffle (coerce your-list 'vector)) 'list))
See the Google Groups archive for ways to implement RANDOM-SHUFFLE
in Common Lisp. [Hint: "Fisher-Yates"...]
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607