Generate a random combination on each line

0

I did a number generator that looked like this:

import random
import math

for i in range(10):

    c1 = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]))
    c2 = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]))
    c3 = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]))
for i in range(90):
 print  ('{}{}{}'.format(c1, c2, c3, ))

It works almost everything, when I run the code in pycharm it generates 90 combinations but all of them are the same but I want them to be random as if I had run the code several times.

    
asked by anonymous 17.11.2018 / 14:22

1 answer

2

What happens is that when you set the random values for c1, c2 and c3 they really were random and different from each other, so the random function fulfilled its role. If you want them to be random inside the for you need, for each iteration, call the random method again.

import random

def gerar_randomico():
    return random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9])

for i in range(3): 
    print ('{}{}{}'.format(gerar_randomico(), gerar_randomico(), gerar_randomico()))
    
17.11.2018 / 14:41