Use uniform balanced binary sequence sampling by choosing the positions of the heads. Number the positions from zero to . Start with no selected positions. Repeatedly toss the fair coin times, interpret the resulting bits as a position, and select that position if it has not already been selected; otherwise discard this block of tosses and retry. Stop when distinct positions have been selected. Put heads at those positions and tails everywhere else.
After positions have been selected, every one of the remaining positions has the same probability of being the next new position. This follows either by summing a geometric series over rejected blocks or by conditioning a uniform distribution on a finite set on avoiding the selected positions. Every ordered list of distinct selected positions consequently has probabilityEach fixed -element subset has such orders, so its probability isThus every balanced binary sequence is equally likely.
When positions are selected, a block succeeds with probability , so the geometric distribution gives expectation blocks until the next new position. By linearity of expectation, the total expected toss count isThe strict inequality holds since , making each term less than two. A Boolean array records membership of each position; initializing it and constructing the output require additional work. The scheme terminates with probability one, since every stage has positive success probability.