import random


def constrained_randint(frac, fg_dim, bg_dim):
    """Return a random int constrained by the allowed trunction fraction and
    bg/fg sizes

    Args:
        frac(float): Max allowed truncation fraction
        fg_dim(int): Foreground dimension (height or width)
        bg_dim(int): Background dimension (height or width)
    """
    return random.randint(int(-frac * fg_dim), int(bg_dim - fg_dim + frac * fg_dim))