if not leading_zeroes:
    return random.randint(10 ** (x - 1), 10 ** x - 1)
elif x > 6000:
    return ''.join([str(random.randint(0, 9)) for i in range(x)])
else:
    return '{0:0{x}d}'.format(random.randint(0, 10 ** x - 1), x=x)
Python: How to generate a 12-digit random number?