+ 2

Making 6 different random Numbers.

I'm not that good at coding right now, i am trying to improve and learn. ATM i was trying to write a code that randomly picks 6 non-repeating numbers, but i fail at it. what should i do? <code> import random a = random.randint(1, 100) b = random.randint(1, 100) c = random.randint(1, 100) x = random.randint(1, 100) y = random.randint(1, 100) z = random.randint(1, 100) outa = b, c, x, y, z outb = a, c, x, y, z outc = a, b, x, y, z outx = a, b, c, y, z outy = a, b, c, x, z outz = a, b, c, x, y all = a, b, c, x, y, z while a in outa or b in outb or c in outc or x in outx or y in outy or z in outz: if a in outa: a = random.randint(1,100) elif b in outb: b = random.randint(1,100) elif c in outc: c = random.randint(1,100) elif x in outx: x = random.randint(1,100) elif y in outy: y = random.randint(1,100) elif z in outz: z = random.randint(1,100) print(all) </code>

26th May 2017, 5:41 PM
Noname Nonamer
Noname Nonamer - avatar
3 ответов
+ 8
You might want to try using set's specifics here and get random unique values in this way: from random import randint as r b = set() while len(b)!=6: b.update({r(1, 100)}) print(b) Set will increment its size only if the random value is different from the ones already in it. Which is pretty much what you want, if I understand it correctly. Moreover, you also get them already sorted this way :)
26th May 2017, 5:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
I don't want to give a direct answer, because this is stuff you have to learn, but you should totally familiarize yourself with for-loops.
26th May 2017, 5:44 PM
Riku Pepponen
Riku Pepponen - avatar
0
@Kuba Siekierzyński exactly what i wanted :) ty. ps: find out that this does the same thing: import random print(random.sample(range(1, 100), 6))
26th May 2017, 6:50 PM
Noname Nonamer
Noname Nonamer - avatar