MediumEvaluationPython 3
Experiment Traffic Splitter
Assign stable experiment variants using weighted deterministic buckets.
30m3 sample tests5 hidden tests
Assign users to weighted experiment variants with stable deterministic bucketing.
Requirements
- Define
assign_variant(user_id, experiment_key, variants). variantsis a list of(name, weight)tuples.- Ignore variants with weight
0. - Use a stable bucket derived from
experiment_keyanduser_id. - Return the selected variant name.
- Return
Noneif total positive weight is0. - Changing
experiment_keycan change assignment. - Calling the function repeatedly with the same inputs must return the same variant.
Example
python
1assert assign_variant("u1", "exp", [("control", 50), ("treatment", 50)]) in {"control", "treatment"}Constraints
- Don't use Python's built-in
hash, because it's process-randomized. - Don't use randomness.
- Preserve variant order for bucket ranges.
Editor