If you wish only somar
the numbers that were generated randomly, you can use the following algorithm...
from random import randint
n = int(input('Desejas gerar quantos números aleatórios? '))
numeros = [randint(1, 100) for _ in range(n)]
soma = sum(numeros)
print(f'\033[32mA soma dos {n} números aleatórios gerados é: {soma}\033[m')
See how the algorithm works on repl it..
Now, if you want to perform the sum of values generated randomly and without repetições
you can use the following algorithm...
from random import sample
n = int(input('Desejas gerar quantos números aleatórios? '))
numeros = sample(range(1, 100), n)
soma = sum(numeros)
print(f'\033[32mA soma dos {n} números aleatórios gerados é: {soma}\033[m')
See how the algorithm works on repl it..
Note that the method sample
class random
, draws without repetitions.
random.sample(range(x), x-y)
also serves if you don’t want duplicates– Miguel