Reference library

A/B testing & experimentation

User bucketing, experiment metrics, statistical comparison, and rollout guardrails.

3 matches
A/B testing & experimentation easy

How to Hash a User ID to an Experiment Bucket in Python

Deterministically map a user ID to one of N experiment buckets using MD5 hashing and modulo arithmetic.

hashing ab-testing bucketing
Python
import hashlib

def hash_to_bucket(user_id: str, num_buckets: int = 10) -> int:
    """Deterministically map a user_id to a bucket (0 to num_buckets-1)."""
    digest = hashlib.md5(user_id.encode("utf-8")).hexdigest()
    return int(digest[:8], 16) % num_buckets

if __name__ == "__main__":
    # Mock experiment: split…
14 0 Open
A/B testing & experimentation easy

How to Mock Stratified Assignment by Segment in Python

Simulate stratified assignment for A/B experiments by sampling a fixed proportion of units from each segment, with deterministic seeds for reproducibility.

ab-testing sampling random
Python
import random

def stratified_assignment(segments, seed=None):
    """
    Mock stratified assignment: given a dict of segment -> population size,
    return a dict of segment -> sampled unit ids (deterministic with seed).
    """
    if seed is not None:
        random.seed(seed)
    rng = random.Random(seed)
    res…
12 0 Open
A/B testing & experimentation easy

How to hash user IDs to experiment buckets in Python

Deterministically map a user ID to an experiment bucket using MD5 hashing, ensuring stable and consistent assignment for A/B testing.

hashing ab-testing experiments
Python
import hashlib


def hash_user_to_bucket(user_id: str, num_buckets: int = 10) -> int:
    """Deterministically map a user ID to an experiment bucket (0..num_buckets-1)."""
    digest = hashlib.md5(user_id.encode("utf-8")).hexdigest()
    return int(digest, 16) % num_buckets


if __name__ == "__main__":
    mock_users …
12 0 Open

Browse by section

Each section groups closely related Python snippets.

A/B testing & experimentation — Python code examples

What you will find here

This page collects a/b testing & experimentation snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.