lundi 9 janvier 2023

Choose random numbers from multiple lists and it must contain at least one number from each list

Let's say I have 3 different lists

A = [1, 2, 3, 4, 5]
B = [11,12, 13, 14, 15]
C = [21, 22, 23, 24, 25]

I want to select 5 random numbers(repeating numbers are fine) from the above lists but it must contain at least one number from each of the lists. Here is my attempted implementation.

from random import choice
for i in range(5): # total numbers selected can be variable
    print(choice(choice([A, B, C]))) #inside choice chooses the list and outside one the number

The above code doesn't necessarily select numbers from each of the lists. How can I select the numbers from the lists ensuring at least one number is chosen from each list?




Aucun commentaire:

Enregistrer un commentaire