Why it sometimes repeat numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
31st Aug 2018, 10:22 PM
Michał P
Michał P - avatar
8 Answers
+ 2
When the numbers are generated, they are not sorted. You only sort them in the very last line. Maybe the numbers are generated like this: number1: 47 number2: 12 (checks if number2 is equal to number1 => it's not, go on) number3: 3 (checks if number3 is equal to number2 => it's not, go on) number4: 12 (checks if number4 is equal to number3 => it's not, go on) !!! number4 is equal to number2, but you don't check that! (same procedure with numbers 5 and 6) In the end, you have [47, 12, 3, 12] and if you sort it [3, 12, 12, 47].
1st Sep 2018, 7:03 AM
Anna
Anna - avatar
+ 2
Basically randint works on timer that starts when you excecute the file (or something like that), so the result are similar every time because the timer starts over and over again and stops at similar point. https://code.sololearn.com/cS69Wl16I2n8/?ref=app
31st Aug 2018, 10:47 PM
Potato Squad
Potato Squad - avatar
+ 1
Never happened when I tested it. but consider a different approach, an array with all values and take out the value once you use it. i.e., take a random element from the array. but what you have, I don't see a problem
31st Aug 2018, 10:26 PM
J.G.
J.G. - avatar
+ 1
You can use a set to make sure that there are no repetitions, or you can use random.sample https://code.sololearn.com/cUeJDd3fhW1D/?ref=app
1st Sep 2018, 6:18 AM
Anna
Anna - avatar
+ 1
Potato Squad You need to compare each number with each other number from the list. Comparing number5 with number4 isn't enough because it could also be equal to number1, number2, or number3. That's why there are still repetitions in your code
1st Sep 2018, 6:22 AM
Anna
Anna - avatar
+ 1
Omg i see it know, You are absolutely right. That sorted function confused me. Thanks so much Anna !
1st Sep 2018, 7:13 AM
Michał P
Michał P - avatar
+ 1
You're welcome! 😁👍
1st Sep 2018, 7:19 AM
Anna
Anna - avatar
0
When i run my code, and the output has repetitions, they are always in pair (1 by 1). Really dont get it why, couse i thought that while loop will loop till the condition numberX == numberY is True and only when it's not the code goes further. So numbers shouldn't repeat and should be excluded. Or am i wrong? Pls explain i'm just beginner.
1st Sep 2018, 6:55 AM
Michał P
Michał P - avatar