-2

I'm trying to create two random lists of 5 numbers between 1-20 and compare them. This is what I got so far:

import random

list1 = []
list2 = []
loop = 0

while loop < 5:
    nmbr1 = random.randint(1, 20)
    nmbr2 = random.randint(1, 20)
    loop += 1
    list1.append(nmbr1)
    list1.sort()
    list2.append(nmbr2)
    list2.sort()

print (list1)
print (list2)

result = sorted(list1) == sorted(list2)

if result == True:
    print("Congratulations, you've won a Lottery!!")

Now, here are some problems that I'm getting:

I'm getting multiple same numbers in the list, for example: ;[3, 3, 5, 13, 16]; I have two "3" in one list. How can I make a condition or something that the same number cannot be randomly created in one list?

I would like to create a while loop that will randomly create two lists until they are a match and then print out this message that I have in my code.

Is it possible to make a function of this number randomizer code so instead of making it like I did it, it is actually a function?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    See [random.sample](https://docs.python.org/3/library/random.html#random.sample). Note that in your current code, sorting the lists every time you append a number is useless, you just need to do it once when comparing them. Note also that instead of sorting the lists, you could convert them to `set`s and test if the sets are equal. – Thierry Lathuille Aug 12 '21 at 13:26
  • 1
    What is the benefit of this function? The result will just be the output of two lists that are the same. Seems a lot of processing for no gain. – trincot Aug 12 '21 at 13:27
  • @trincot looks like a homework type problem. – blackbrandt Aug 12 '21 at 13:41
  • I would use a while False (while not True) loop – pippo1980 Aug 12 '21 at 13:41
  • @ThierryLathuille thanks, I will check it out. -tricot The function I want to create or? I would just like to learn functions and would like to know how to create one from this code above. -pippo1980 yeah that is probably better – Mirkopoter Aug 12 '21 at 14:33
  • Does this answer your question? [Generate 'n' unique random numbers within a range](https://stackoverflow.com/questions/22842289/generate-n-unique-random-numbers-within-a-range) – Tomerikoo Aug 12 '21 at 15:42
  • @Tomerikoo It does answer part of my question, good to know about random.sample for any future projects. Thanks :) – Mirkopoter Aug 13 '21 at 05:41

3 Answers3

2

here my attempt, let me know if it works right as expected from You, given the: 'Create two random lists of 5 numbers between 1-20 and compare them until they are a match' question title

import random



check = False

attempts = 0
while not check:
    list1 = []
    list2 = []
    loop = 1
    while loop <= 5:
        nmbr1 = random.randint(1, 20)
        nmbr2 = random.randint(1, 20)
        
        list1.append(nmbr1)
        list1.sort()
        list2.append(nmbr2)
        list2.sort()
        loop += 1

    print (list1)
    print (list2)

    if list1 == list2:
        check = True
    else:
        pass
    loop = 0
    attempts += 1


print("Congratulations, you've won a Lottery!! in ",attempts, "attempts")

here I got @Thierry Lathuille suggestion of comparing set() of lists:

import random



check = False

attempts = 0
while not check:
    list1 = []
    list2 = []
    loop = 1
    while loop <= 5:
        nmbr1 = random.randint(1, 20)
        nmbr2 = random.randint(1, 20)
        
        list1.append(nmbr1)
        list2.append(nmbr2)
        loop += 1

    print (list1)
    print (list2)

    if set(list1) == set(list2):
        check = True
    else:
        pass
    loop = 0
    attempts += 1


print("Congratulations, you've won a Lottery!! in ",attempts, "attempts")

third version, now the two lists don't accept duplicate numbers

import random



check = False

attempts = 0
while not check:
    list1 = []
    list2 = []

    while len(list1) <5:
        nmbr1 = random.randint(1, 20)
        if nmbr1 not in list1:
            list1.append(nmbr1)
    while len(list2) <5:
        nmbr2 = random.randint(1, 20)
        if nmbr2 not in list2:
            list2.append(nmbr2)
        
    print (list1)
    print (list2)

    if set(list1) == set(list2):
        check = True
    else:
        pass

    attempts += 1


print("Congratulations, you've won a Lottery!! in ",attempts, "attempts")

let me know if it works

pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • This works great except one problem, the list that are randomly generated can have same numbers, example: [1, 9, 9, 15, 18], I would like to avoid that so that the numbers are always different. Still, thank you :) – Mirkopoter Aug 12 '21 at 14:16
  • updated wit @Thierry Lathuille suggestion of using set() so you get printed the actual lists and not the sorted version of them, please accept or upvote my answer if suits your needs – pippo1980 Aug 12 '21 at 14:34
  • I cannot upvote because I'm a new member and I cannot really accept as an answer (because of people who will read this is the future) because the list generator is generating duplicate numbers like this: [1, 9, 9, 15, 18], there are two 9's here and that is what I'm trying to avoid. – Mirkopoter Aug 12 '21 at 14:50
  • okkey now I got it it wasnt really clear to me the list that are randomly generated can have same numbers was intended as the list that is randomly generated cannot have two equal numbers, just check it before appending to the list – pippo1980 Aug 12 '21 at 14:54
  • @Mirkopoter new version updated as per your request – pippo1980 Aug 12 '21 at 15:02
  • @pippo1989 it works now, it was a quite simple if statement and I was trying so much more complicated stuff to try to achieve that.. thank you for your effort, I accepted your answer as correct :) – Mirkopoter Aug 13 '21 at 05:28
0

hope you will like these codes :)

import random

list1 = []
list2 = []
loop = 0
result = False

while loop < 5 and result != True:
    while True :
        rand = random.randint(1, 20)
        if rand not in list1 :
            break
    list1.append(rand) 
    list1.sort()
    while True :
        rand = random.randint(1, 20)
        if rand not in list2 :
            break  
    list2.append(rand)
    list2.sort()    
    for i in list1 :
        if i in list2 :
            result = True

    loop += 1

print (list1)
print (list2)

if result == True:
    print("Congratulations, you've won a Lottery!!")
  • Unfortunatelly this is not working, sometimes the code will make less than 5 numbers like this: [1, 7, 15, 19] [5, 7, 14, 15], also, sometimes the code just stops like on this: [4, 7, 9, 11, 12] [3, 6, 14, 15, 18], it is not going on until it gets to a match. – Mirkopoter Aug 12 '21 at 14:18
0

The problem with generating the numbers randomly this way in a loop is that you'll get duplicates.

A way to avoid this is to remove each ball you draw from the pool of balls before drawing the next one.

Another way is to check that the last ball you drew hasn't been drawn before. If it has, choose another one randomly until you get one that you haven't got already. This works well if you take a small sample compared to the whole pool of balls, but could take much time if you draw a large part of them all.

The simplest solution though is to use the random.sample function which is designed to do just that:

random.sample(population, k, *, counts=None)

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

In order to compare your draws, you can use sets, which are equal if they contain the same unique items, whatever the order. (Note that your idea of sorting the lists can be useful if you have duplicates and want to check that you have the same number of each item. Another option would be to use a collections.Counter in this case)

So, the code would be (with 6 balls only here, in order to have a chance to win ;) ):

import random

balls = range(1, 7)  # numbers from 1 to 6 included, for an easier win 
number = 5

draw1 = random.sample(balls, number)
draw2 = random.sample(balls, number)

print(draw1)
print(draw2)

if set(draw1) ==  set(draw2):
    print("Congratulations, you've won the lottery!!")

Lucky output:

[4, 6, 1, 5, 3]
[3, 5, 6, 4, 1]
Congratulations, you've won the lottery!!
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • 1
    This works but not quite like I imagined it. This solution does not create two lists through a while loop until the match is found. The idea is that the program, when run, should be creating two lists without stopping until the two lists match and it should stop and print the output message. – Mirkopoter Aug 13 '21 at 05:37
  • Well, as already stated in the comments under your question, this makes no sense. You would only have a piece of code that would spend much time generating useless lists until you get a match (which has a probability of 1/38760 to happen each time). Put that code in a function and call it as you need. Note that this is the only clean, recommended way to take samples without repetitions. – Thierry Lathuille Aug 13 '21 at 13:13