import random


def variate(mean_ms, delay_max_ms):
    """
    Generate delay following a gamma distribution.
    """
    while True:
        x = mean_ms * random.gammavariate(2.0, 0.5)
        if x <= delay_max_ms:
            return x