Import random | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Import random

#range(6), random.randint(1, 49), : whats else it needs to stop printing a repeat nums? in range of 6

23rd Sep 2017, 7:59 AM
5p4c353c
5p4c353c - avatar
16 Answers
+ 2
Mohamed, why the trailing `else` clause at the end of the `while` block? It's doing essentially nothing. Anyway, kenji, what about this? from random import randint seen = set() while len(seen) < 6: seen.add(randint(1, 49)) print(seen)
24th Sep 2017, 7:39 PM
Edgar Garrido
Edgar Garrido - avatar
+ 2
from random import randint a = [] for i in range(6): x = randint(1, 49) if x not in a: a. append(x) print(a)
23rd Sep 2017, 12:59 PM
Mohammed Chami
Mohammed Chami - avatar
+ 2
from random import randint a = [] counter = 0 while counter < 6: x = randint(1, 49) if x not in a: a. append(x) counter +=1 else: pass print(a)
24th Sep 2017, 10:22 AM
Mohammed Chami
Mohammed Chami - avatar
+ 2
thx a lot DZ, now I know
24th Sep 2017, 11:22 AM
5p4c353c
5p4c353c - avatar
+ 2
you are welcome :)
24th Sep 2017, 11:50 AM
Mohammed Chami
Mohammed Chami - avatar
+ 2
I knew that, I just wanted to do that for him to see if he will notice that or not hhhh that's all.
24th Sep 2017, 8:30 PM
Mohammed Chami
Mohammed Chami - avatar
+ 1
Use a set comprehension, sets can't have repeated values, i.e., from random import randint uniques = {randint(1, 49) for _ in range(6)}
23rd Sep 2017, 2:32 PM
Edgar Garrido
Edgar Garrido - avatar
+ 1
DZ seems The best ans, but the output is sometimes 4 nums or 5 nums only in range(6), can't get it?
24th Sep 2017, 7:29 AM
5p4c353c
5p4c353c - avatar
+ 1
Ah lol I see, I ruined it.
24th Sep 2017, 8:35 PM
Edgar Garrido
Edgar Garrido - avatar
0
import random for i in range (6): print ( random.randint(1, 49)) #output, 3 , 15 , 30, 3, 16, 47 #how can it not output a num two times, e.g '3'?
23rd Sep 2017, 11:07 AM
5p4c353c
5p4c353c - avatar
0
import random arr=[] f=0 while True : a=random.randint(1,49) if a not in arr : print(a) arr.append(a) f+=1 if f==6 : break
23rd Sep 2017, 11:37 AM
sayan chandra
sayan chandra - avatar
0
@ kenji i posted an answer before... its seems perfect .... it always prints 6 different numbers
24th Sep 2017, 9:25 AM
sayan chandra
sayan chandra - avatar
0
hah got me! I just a newbie, wondering the random function should have some kind of methods to do all the work not repeated the nums, would it be without a loops.
25th Sep 2017, 5:56 AM
5p4c353c
5p4c353c - avatar
0
Edged yours more clean, thx a lot!
25th Sep 2017, 5:57 AM
5p4c353c
5p4c353c - avatar
0
#how about this import random num = list (range (1, 50)) picks = random.sample (num, k=6) print (picks)
25th Sep 2017, 9:28 AM
5p4c353c
5p4c353c - avatar
- 1
exactly what u want be more specific please
23rd Sep 2017, 11:00 AM
sayan chandra
sayan chandra - avatar