help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: random predicate function


From: Tyler Smith
Subject: Re: random predicate function
Date: Mon, 13 Dec 2010 13:17:04 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

>
> You shoud not use sort to randomize, because it's suboptimal
> [ O(n*log(n)) at best instead of O(n) ].
>
> And foremost, you should not use a predicate that is not a total order
> because this usually gives invalid results.

I don't know what a 'total order' means. Is the result of the predicate
invalid or the actual sorting?

>
> Google for: shuffle algorithm.

Thanks. I knew there must be a name for what I was trying to do.

> You could instead put your paragraphs in a vector and use:
>
> (defun shuffle (vector)
>   "Re-orders randomly the vector."
>   (loop
>       for i from (1-  (length vector)) downto 1
>       do (rotatef (aref vector i) (aref vector (random i)))))
>
> to shuffle them and then re-insert them.

Thanks for this! Very helpful.

Tyler




reply via email to

[Prev in Thread] Current Thread [Next in Thread]