Using only Bernoulli trials with $p=0.5$, how can I efficiently create a permutation of length $k$ from a set of length $n$?
I can think of a few ways to do this that waste trials, but I’d like to do so efficiently (using every trial). For example, the best method I can think of is (using the Fisher-Yates shuffle):
If $n=5$
For $i$ in $[0,k-1]$:
- Conduct $ceil(log_2(n))$ trials.
$1,1,0$
- Convert the series of trials into a binary number, $q$.
$q=b110=6$
- Perform the Fisher-Yates step to find the next term in the permutation using the swap index: $q \mod (n-i)$.
Intuitively, this feels wasteful by at most 2 times the number of trials required. Thanks in advance!
You can generate efficiently (with negligible loss of entropy, asymptotically) samples from a $\ell$-sided die using fair coin tosses: See [1], [2]. These samples can then be used in the Fisher-Yates shuffle.
To describe a simple procedure, pick a large $m$ and find $n$ such that $2^{n-1}< \ell^m \le 2^n$. Pick $n$ bits, yielding a uniform $X$ in $\{0,1,\ldots, 2^n-1\}$ and reject $X$ if $X \ge \ell^m$; otherwise, $X$ yields a sample of $m$ independent uniform variates in $\{0,1,\ldots, j-1\}$; only one bit of entropy is lost for each such sample.
[1] Knuth, Donald E., and Andrew C. Yao. "The complexity of nonuniform random number generation. Algorithms and complexity (Proc. Sympos., Carnegie-Mellon Univ., Pittsburgh, Pa., 1976), 357–428." (1976): 357-428.