Generate an int with nbits random bits.

    Raises ValueError if nbits <= 0, and TypeError if it's not an integer.

    >>> randint(16)  #doctest:+SKIP
    1871

def randint(nbits: int) -> int:
    """Generate an int with nbits random bits.

    Raises ValueError if nbits <= 0, and TypeError if it's not an integer.

    >>> randint(16)  #doctest:+SKIP
    1871

    """
    if not isinstance(nbits, int):
        raise TypeError('number of bits should be an integer')
    if nbits <= 0:
        raise ValueError('number of bits must be greater than zero')

    # https://github.com/python/cpython/blob/3.6/Lib/random.py#L676
    nbytes = (nbits + 7) // 8                       # bits / 8 and rounded up
    num = int.from_bytes(randbytes(nbytes), 'big')
    return num >> (nbytes * 8 - nbits)