import random


def rnd(q):
    """ Returns a random number less than q, with the same bits length than q

    Args:
        q (int)         : field/modulo

    Returns:
        int : random 
    """
    nbits = q.bit_length()
    while True:
        k = random.getrandbits(nbits)
        if k<q:
            return k