N
N
NOblo2020-08-07 15:23:39
Python
NOblo, 2020-08-07 15:23:39

Random in python, how to generate a number?

It is necessary not only to generate a number, it is necessary that such a number is not in the list
For example:

j = 10, 20, 30
randint(1, 40)
#нужно сделать так, чтобы числа из кортежа j никак не могли сгенерироваться

?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SKEPTIC, 2020-08-07
@NOblo

import random


def gen_num(min, max, bads):
    while True:
        rand = random.randint(min, max)
        if rand not in bads:
            return rand

j = 1, 2, 3, 4
print(gen_num(1, 40, j))

M
Maxim Siomin, 2020-08-07
@MaxSiominDev

import random

j = [10, 20, 30]


def generateNum(j, minNum, maxNum):
    generatedList = []

    for x in range(minNum, maxNum):
        if x not in j:
            generatedList.append(x)


    num = random.choice(generatedList)
    return num

generateNum(j, 1, 40)

The answer from below is of course simpler, but there is not quite a clean generation

D
Dr. Bacon, 2020-08-07
@bacon

Greetings from kindergarten
1. generate,
2. check
3. if there is, then point 1

N
nnikolyaa, 2020-08-07
@nnikolyaa

Well, it's kind of. If you really cut it down to atoms.

random_number = random.randint(1, 40)
while random_number!=ЧТО НАДО УБИРАТЬ:
    random_number = random.randint(1, 40)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question