Sub topic 5
Assigning Recipients to Variants at Random
This chapter assumes you know what a hypothesis and a primary metric are, and that you can read short Python snippets. Random assignment is what lets you say a difference in results was caused by your change and not by who received it.
Why randomness matters
If you send variant A to your newest subscribers and variant B to your oldest, any difference in clicks could come from the audiences. Random assignment spreads every other factor evenly across variants. That includes engagement history, location, and device. Any factor you did not think of is balanced too.
Common ways to break randomness:
- Splitting the list alphabetically or by signup date.
- Sending variant A to the first half of an export, which is often sorted.
- Letting a person pick "the best customers" for one variant.
- Assigning by email domain or region, which mixes audience with treatment.
Reproducible assignment with hashing
You could shuffle the list and cut it in half. That works, but it is hard to repeat. If you rerun the job, people may switch variants, and you cannot audit who saw what later.
A hash function gives a stable alternative. Hash the user ID together with the experiment name, then use the remainder to pick a variant. The same input always gives the same result. Because the experiment name is part of the key, the same person falls into an unrelated bucket in a different experiment.
import hashlib
def assign_variant(user_id, experiment, variants=("A", "B")):
key = f"{experiment}:{user_id}".encode("utf-8")
digest = hashlib.sha256(key).hexdigest()
return variants[int(digest, 16) % len(variants)]
print(assign_variant("user-123", "subject-test-2026-11"))The result is effectively random across people but fixed for any one person. You can recompute it at analysis time without storing a table.
Checking the split
Hashing gives roughly equal groups, not exactly equal ones. With 40,000 recipients, a 50/50 split might give 20,112 and 19,888. That is normal chance variation. A large gap is a warning sign. For example, a 55/45 split on a big list suggests a bug, such as a filter applied after assignment or duplicate IDs.
Also compare the groups on things you already know before the send, such as past click rate or signup age. They should look alike. If they do not, fix the assignment before you send.
Pitfalls
- Unstable IDs. Hash a permanent ID. If you hash an email address that users change, the same person can move between variants.
- Assigning after exclusions. Remove suppressed and unsubscribed addresses first, or compute assignment for everyone and drop them afterward. Do not let a filter hit one variant harder than the other.
- Reusing the experiment name. If you reuse a name, you reuse the same split. People who always land in B will always see B's treatment across tests, so the tests are not independent.
- Changing variants mid-test. Reassigning people halfway contaminates both groups.
Exercise
You hash only the user ID, without the experiment name, and use the result to split recipients for every test. What problem does this cause?
Show answer
Each person lands in the same bucket in every experiment. The same half of your list is always variant A, so one test's effects can carry into the next, and the tests are not independent. Including the experiment name in the hash key gives each test its own unrelated split.
Exercise
A 50/50 test of 40,000 recipients ends up with 24,000 in variant A and 16,000 in variant B. What should you do?
Show answer
Treat it as a bug and do not trust the results. A gap this large is far beyond chance. Look for a filter or exclusion that was applied after assignment, or for duplicate IDs, and fix it before sending.