TITLE: Random Alternating Permutations
QUESTION [27 upvotes]: An alternating permutation of {1, ..., n} is one were π(1) > π(2) < π(3) > π(4) < ... For example: (24153) is an alternating permutation of length 5.
If $E_n$ is the number of alternating permutations of length n, the $\sec x + \tan x = \sum_{n \geq 0} E_n \frac{x^n}{n!}$ is the exponential generating function.
How does one sample the alternating permutations uniformly at random?  Rejection sampling would quickly become inefficient since $\frac{E_n}{n!} \approx \frac{4}{\pi} \left(\frac{2}{\pi} \right)^n$ decays exponentially in n.

REPLY [11 votes]: An $O(n \log n)$ algorithm to uniformly sample alternating permutation is described by P. Marchal in *Generating alternating permutations in time $O(n \log n)$." (by contrast, Pak's answer is $O(n^3),$ Boltzmann sampling is $O(n^2).$