Answer the question
In order to leave comments, you need to log in
How to limit the range of generated numbers?
How to limit the range of generated numbers in this random function ? Let's say I want 3 numbers ( lenght = 3 ), however these 3 numbers should not exceed 700 , how to do it and is it possible to do it without checking for
if test < 700
length = 3
test = ''.join([str(random.randint(1, 9)) for x in range(length)])
Answer the question
In order to leave comments, you need to log in
def generate_number(length, maximum):
num = int(''.join([str(random.randint(1, 9)) for _ in range(length)]))
if num < maximum:
return num
else:
return generate_number(length, maximum)
length = 3
test = '{}{}'.format(random.randint(1,6), ''.join(str(random.randint(1,9)) for _ in range(length-1)))
length = 3
test = ''.join([str(random.randint(1, 700)) for x in range(length)])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question