import torch


def sample_truncated_gumbel(mu, b, eps=1e-10):
    """Sample a Gumbel(mu) truncated to be less than b."""
    u = torch.rand_like(mu)
    return -torch.log(eps - torch.log(u + eps) + torch.exp(-b + mu)) + mu