C
C
crinnyx2021-03-24 21:10:32
Python
crinnyx, 2021-03-24 21:10:32

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

3 answer(s)
S
soremix, 2021-03-25
@crinnyx

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)

Well, or without if
length = 3
test = '{}{}'.format(random.randint(1,6), ''.join(str(random.randint(1,9)) for _ in range(length-1)))

crinnyx done

B
bryuliks don't care how old you, 2021-03-24
are @l3m5

length = 3
test = ''.join([str(random.randint(1, 700)) for x in range(length)])

?

H
HemulGM, 2021-03-24
@HemulGM

random.randint(100, 700)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question