Here is the background for this question. Friends and I were playing a game where everyone needs to give another people some gift. In order to determine who should give gift to whom, we decide to drew lots. But the problem is, someone might end up giving him/herself gifts, which is not funny. You can see that the expected number of such unfortunate people is 1, so this happens quite frequently.

For this purpose, dearrangement seems to be a great fit. If I can fairly generate a dearrangement, then I can just pick one dearrangement and use it to decide who give whom gifts.

Randomized dearrangement generation could be done with Las Vegas method. But the problem is, it only has expected polynomial running time. So I came to this problem of finding i-th dearrangement. If I can randomly pick an i in [1, D_n], and use some worst case polynomial time (efficient) algorithm to obtain the i-th dearrangement, then it's done.

  • 1
    $\begingroup$ Could you please explain the motivation for the question? i.e. why are you interested in this question? $\endgroup$
    – Kaveh
    Commented Dec 10, 2012 at 4:43
  • 2
    $\begingroup$ Perhaps you want to play secret santa and aren't willing to take any chances :) $\endgroup$
    – Lev Reyzin
    Commented Dec 11, 2012 at 23:01
  • $\begingroup$ Could you add a line about what you mean by dearrangement? $\endgroup$
    – Vijay D
    Commented Dec 27, 2012 at 2:14

2 Answers 2


Actually this is might be good question but it is badly formulated in its current form. The well-known algorithms for generating random derangements have linear expected time, but maybe it's an open problem to find a worst case polynomial time algorithm.

See for example: http://www.siam.org/proceedings/analco/2008/anl08_022martinezc.pdf (and slides: http://www.lsi.upc.edu/~conrado/research/talks/analco08.pdf)

  • $\begingroup$ This seems like the right answer to me. $\endgroup$ Commented Dec 10, 2012 at 1:13
  • 10
    $\begingroup$ Doesn't the recurrence !n=(n−1)(!(n−1)+!(n−2)) described in en.wikipedia.org/wiki/Derangement immediately lead to a worst-case polynomial algorithm for random generation? $\endgroup$ Commented Dec 10, 2012 at 3:16
  • $\begingroup$ Yes, you are right. I was thinking there is a small catch because you have to be able to generate random numbers in arbitrary subsets of {1,...,n} in worst-case polytime, but that's easy to do. $\endgroup$
    – didest
    Commented Dec 10, 2012 at 14:37

Why not, for each position i, randomly choose from all elements other than i? For example, you could choose an index into the original array from [0..n-2], and if you get j >= i you use j+1.

  • 3
    $\begingroup$ Does that make all derangements equally likely? $\endgroup$ Commented Dec 9, 2012 at 23:39
  • $\begingroup$ oh, good point - this would place elements later in the array preferentially earlier in the array. If you were to fill slots in the target array in a random order all derangements would then be equally likely (by symmetry). $\endgroup$
    – pat
    Commented Dec 9, 2012 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.