3

Input:

import random
I = 0
z = []
while I < 6:
    y = random. Choices(range(1,50))
    if y in z:
        break
    z += y
    I += 1
print(z)

Output: [8, 26, 8, 44, 31, 22]

I'm trying to make a Lotto numbers generator but I can't make the code to generate 6 numbers that do not repeat. As you can see in the Output 8 is repeating.

I'm not clear why in the If statement the code does not check if the random y variable is already in the z list.

3
  • 4
    Use continue instead of break to continue the loop from the top again. Commented Sep 7, 2022 at 11:25
  • choices returns a list. Checking whether a list exists in a list will (in your case) always be false.
    – deceze
    Commented Sep 7, 2022 at 11:25
  • Not capital" C". Try this y = random.choices(range(1,50)) Commented Sep 7, 2022 at 11:31

5 Answers 5

8

Use random.sample:

>>> from random import sample
>>> sample(range(1, 50), k=6)
[40, 36, 43, 15, 37, 25]

It already picks k unique items from the range.

5

Check out this code, it works pretty well for me.

  1. since choices returns a list, I used choice instead to return a number.
  2. break stops the code so I used continue instead.
  3. variable I can be omitted using len(z) in while statement. (as suggested by Mime)

.

import random
# I = 0
z = []
while len(z) < 6: # instead of I < 6: (Thanks to Mime)
    y = random.choice(range(1,50)) # edit: you can also use random.randint(1,49) instead of choice.
    if y in z:
        continue
    z.append(y) # instead of z += [y] (Thanks to deceze)
    # I += 1
print(z)
2
  • 1
    You could/should use z.append instead of += a list…
    – deceze
    Commented Sep 7, 2022 at 11:29
  • 1
    You could also use while len(z) < 6 and therefore omit the variable I
    – Mime
    Commented Sep 7, 2022 at 11:33
2

break will, well, break out of the loop.

Use continue to continue the loop from the top again, and make sure you pick the first (and only) element returned from random.Choices:

while I < 6:
    y = random.choices(range(1,50))[0]
    if y in z:
        continue # skip to next iteration and continue
    z.append(y)
    I += 1

Or better, use random.randint() to pick the integer instead of expanding the full range every time:

while I < 6:
    y = random.randint(1,50)
    if y in z:
        continue # skip to next iteration and continue
    z.append(y)
    I += 1
4
  • That alone won't fix it. In fact, the break would prevent duplication, if the if condition worked…
    – deceze
    Commented Sep 7, 2022 at 11:27
  • @deceze Thanks, I just noticed. Answer updated Commented Sep 7, 2022 at 11:27
  • Now choices is a bad choice (pun intended) for the task at hand…
    – deceze
    Commented Sep 7, 2022 at 11:28
  • line z+=y gives you an error int object is not iterable. Also you misspelled choices
    – Sharim09
    Commented Sep 7, 2022 at 11:30
2

You can also use walrus (:=) operator and check if y is not in z:

from random import randint
z = list()
while len(z) < 6:
    if (y := randint(1,50)) not in z: z += [y]
print(z)
1

You should do multiple things:

  1. use random.randint()
  2. use .append() for arrays and not +=
  3. use continue instead of break

Your code can look like this:

import random
I = 0
z = []
while I < 6:
    y = random.randint(1,50)
    if y in z:
        continue
    z.append(y)
    I += 1
print(z)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.