June 15, 2007 13:41 WSPC - Proceedings Trim Size: 9in x 6in Derangfinale 1 AN ANALYSIS OF A SIMPLE ALGORITHM FOR RANDOM DERANGEMENTS DONATELLA MERLINI and RENZO SPRUGNOLI and MARIA CECILIA VERRI Dipartimento di Sistemi e Informatica Viale Morgagni, 65 - Firenze (Italy) E-mail: [merlini,sprugnoli]@dsi.unifi.it, mariacecilia.verri@unifi.it We consider the uniform generation of random derangements, i.e., permuta- tions without any fixed point. By using a rejection algorithm, we improve the straight-forward method of generating a random permutation until a de- rangement is obtained. This and our procedure are both linear with respect to the number of calls to the random generator, but we obtain an improvement of more than 36%. By using probability generating functions we perform an exact average analysis of the algorithm, showing that our approach is rather general and can be used to analyze random generation procedures based on the same rejection technique. Moreover, emphasis is given to combinatorial sums and a new interpretation of a known infinite lower triangular array is found. Keywords : Derangements, rejection algorithms, probability generating func- tions. 1. Introduction We consider the random, uniform generation of derangements, i.e., permu- tations without any fixed point. They were introduced during the XVIII century and Euler [3] describes the corresponding counting problem in this way: “Data serie quotcunque litterarum a, b, c, d, e etc., quorum numero sit n, invenire quot modis earum ordo immutari possit, ut nulla in eo loco reperiatur, quem initio occupaverat”. Another famous formulation is as fol- lows. Ten mathematicians arrive at the Faculty Club and leave their hats at the wardrobe. When they go out, everyone takes a hat at random; math- ematicians are notoriously absent-minded. What is the probability that no one takes his own hat? If we mark the mathematicians and their hats with the numbers from 1 to 10, on exit the Club we obtain a permutation in the symmetric group S 10 , and a mathematician who takes his own hat is a fixed point. Therefore, the required probability is D 10 /10!, where D n = |D n | if