def buffered_bounded_lemire_uint16(bitgen, rng, bcnt, buf):
    """
    Generates a random unsigned 16 bit integer bounded
    within a given interval using Lemire's rejection.

    The buffer acts as storage for a 32 bit integer
    drawn from the associated BitGenerator so that
    multiple integers of smaller bitsize can be generated
    from a single draw of the BitGenerator.
    """
    # Note: `rng` should not be 0xFFFF. When this happens `rng_excl` becomes
    # zero.
    rng_excl = np.uint16(rng) + np.uint16(1)

    assert (rng != 0xFFFF)

    # Generate a scaled random number.
    n, bcnt, buf = buffered_uint16(bitgen, bcnt, buf)
    m = np.uint32(n * rng_excl)

    # Rejection sampling to remove any bias
    leftover = m & 0xFFFF

    if (leftover < rng_excl):
        # `rng_excl` is a simple upper bound for `threshold`.
        threshold = ((np.uint16(UINT16_MAX) - rng) % rng_excl)

        while (leftover < threshold):
            n, bcnt, buf = buffered_uint16(bitgen, bcnt, buf)
            m = np.uint32(n * rng_excl)
            leftover = m & 0xFFFF

    return m >> 16, bcnt, buf

    Generates a random unsigned 16 bit integer bounded
    within a given interval using Lemire's rejection.

    The buffer acts as storage for a 32 bit integer
    drawn from the associated BitGenerator so that
    multiple integers of smaller bitsize can be generated
    from a single draw of the BitGenerator.
    
numba/np/random/new_random_methods.py