Simulate a Ramp Rollout Percentage in Python
Simulates a percentage-based ramp rollout with deterministic seeding, returning success/failure/in-progress counts for a mock user population.
Python code
39 linesimport random
from enum import Enum
class RolloutStatus(Enum):
SUCCESS = "success"
FAILED = "failed"
IN_PROGRESS = "in_progress"
def simulate_ramp_rollout(total_users: int, percentage: int, seed: int = 42) -> dict:
"""
Simulates a mock ramp rollout for a given percentage of users.
Returns statistics about the rollout status distribution.
"""
random.seed(seed)
target_count = int(total_users * percentage / 100)
users = list(range(total_users))
in_target_group = set(random.sample(users, target_count))
results = []
for user in users:
if user in in_target_group:
status = random.choices(
[RolloutStatus.SUCCESS, RolloutStatus.FAILED],
weights=[95, 5]
)[0]
else:
status = RolloutStatus.IN_PROGRESS
results.append(status)
status_counts = {status.value: results.count(status) for status in RolloutStatus}
status_counts["total_users"] = total_users
status_counts["percentage_rolled_out"] = percentage
return status_counts
if __name__ == "__main__":
stats = simulate_ramp_rollout(total_users=1000, percentage=25)
for key, value in stats.items():
print(f"{key}: {value}")
Output
success: 237
failed: 13
in_progress: 750
total_users: 1000
percentage_rolled_out: 25
How it works
The function first computes the number of users to target using total_users * percentage / 100. It then seeds the random generator to make results reproducible, and uses random.sample to pick a unique set of target users without replacement. A 95/5 weighted choice assigns each target user a SUCCESS or FAILED status, while non-target users stay IN_PROGRESS. Counts are derived per status via list.count, and the final dictionary includes both total_users and percentage_rolled_out for clarity.
Common mistakes
- Forgetting to reset the random seed, making runs non‑reproducible
- Using `random.choices` on the full population instead of only the selected sample
- Mixing up `random.choice` (single pick) with `random.choices` when weighted selection is needed
Variations
- Use `random.sample` with a generator expression for statuses, then `collections.Counter` to aggregate counts
- Add a parameter for custom success/failure weights to simulate different risk profiles
Real-world use cases
- Modeling incremental feature rollouts to validate monitoring and error‑tracking before full release.
- Estimating load and error budgets during canary deployments in production environments.
- Creating deterministic test fixtures for A/B testing frameworks to verify user assignment logic.
Sponsored
More from A/B testing & experimentation
Keep learning
Related tutorials and quizzes for this topic.