1

Given $n \in \mathbb{Z}^+$, I would like to uniformly sample $k \in \{0 \dots n\}$, then uniformly sample $a_{1 \dots n} \in \{0,1\}^n$ under the constraint that $\sum{a_i}=k$. The auxiliary number $k$ is ignored from the final result.

For example, with $n=2$, the pmf would be $P(00)=1/3, P(01)=P(10)=1/6, P(11)=1/3$. In another word, it's just uniformly random bits with "flattened" outcome distribution that is otherwise binomial.

The naive method is to uniformly sample $k$, let $a=\{1\}^k\{0\}^{n-k}$, then randomly permute it, but the sampling of random permutation seems cumbersome and weird for the task of generating bits.

I would like to ask: 1) if there's a more efficient, coin-tossing-like sampling algorithm that doesn't involve permutations; 2) or more generally, if there's an established mathematical concept for the "flattening" of distributions described above.

1 Answers1

2

Indeed there is such an algorithm. The proof at Why are all subset sizes equiprobable if elements are independently included with probability uniform over $[0,1]$? shows that if you first uniformly randomly draw $p$ from $[0,1]$ and then choose $1$ with probability $p$ for each index, you get exactly the distribution you want.

However, while this may look more elegant and appropriate to the task, it doesn’t actually require fewer random numbers than your permutation approach, which can also be implemented with $n+1$ random numbers (see Fisher–Yates shuffle).

joriki
  • 238,052