Sub topic 14
The Inside-Out Variant
This chapter assumes you know the in-place Fisher-Yates shuffle from earlier chapters, the induction proof of its uniformity, and how Python's random.randint(a, b) includes both ends.
The in-place shuffle rearranges the list you hand it. Sometimes you can't allow that. The input might be shared with other code. It might be a tuple, or a generator that you can read only once. Its length might be unknown until the stream ends. The inside-out variant handles all of these cases. It reads the source a single time, from front to back, and builds a new shuffled list as it goes.
How It Works
The variant keeps one invariant: after it reads item i, the list result is a uniformly random ordering of the first i + 1 items. Each new item x goes to a random position j in 0..i. Whatever was already at j moves to the new last slot.
import random
def inside_out_shuffle(source):
result = []
for i, x in enumerate(source):
j = random.randint(0, i)
if j == i:
result.append(x)
else:
result.append(result[j])
result[j] = x
return result
print(inside_out_shuffle("ABCDE"))
print(inside_out_shuffle(n * n for n in range(6))) # works on a generatorThe j == i branch is needed because result[i] doesn't exist yet. The new item simply stays at the end. In the other branch, the displaced item is copied to the end first, and then the slot it left is overwritten.
A Worked Trace
Here the source is A, B, C, D, with the random draws fixed so you can follow each step:
| i | x | j | result after step |
|---|---|---|---|
| 0 | A | 0 | [A] |
| 1 | B | 0 | [B, A] |
| 2 | C | 2 | [B, A, C] |
| 3 | D | 1 | [B, D, C, A] |
Why It Is Uniform
The proof uses induction, in the same way as the proof for the in-place version. Suppose result holds each of the i! orderings of the first i items with equal probability. Step i picks one of i + 1 positions for the new item. Every pair (old ordering, j) produces a different ordering of i + 1 items, and every ordering of i + 1 items comes from exactly one such pair. That gives i! × (i + 1) = (i + 1)! equally likely outcomes, so the invariant still holds after step i.
Costs and Common Errors
| Property | In-place | Inside-out |
|---|---|---|
| Modifies input | Yes | No |
| Extra space | O(1) | O(n) for the output |
| Needs length up front | Yes | No |
| Time | O(n) | O(n) |
The usual mistake is the range of j. If you write random.randrange(i), which excludes i, the new item can never stay in the last slot. That is the same bound change that turns Fisher-Yates into Sattolo's algorithm, which a later chapter covers. With randrange(i), the first step also crashes, because randrange(0) has no values to choose from. A second mistake is overwriting result[j] before copying its old value to the end. When that happens, an item gets duplicated and another item disappears.
Exercise
Trace insideoutshuffle on the source X, Y, Z with the draws j = 0, 1, 0 for i = 0, 1, 2. What is the final result?
Show answer
i=0: [X]. i=1, j=1: append Y to get [X, Y]. i=2, j=0: append result[0] = X, then set result[0] = Z, giving [Z, Y, X].
Exercise
A generator yields items you cannot rewind, and you don't know how many there are. Why can't you use the in-place shuffle directly, and how many random draws does the inside-out variant make for 7 items?
Show answer
The in-place version needs an existing, indexable list of known length, and it starts at the end. Using it would mean copying the stream into a list first. The inside-out variant makes 7 draws, one per item. The first draw is always 0, so only 6 of them affect the result.