Making a list of randomly selected items

Question:

I nee to make a list or tuple containing a series of 10 numbers and five letters. randomly select four numbers or letters from the list and print a message saying that any ticket matching these four numbers or letters wins a prize.

code I tried but doesn’t know what to write ahead:

possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e']

winner = []

print("winning ticket is...")

while len(winner) > 4:
    pulled_item = choice(possibilities)

    if pulled_item not in winner:
        print(f"we pulled a {pulled_item}!")
        winner.append(pulled_item)

pls provide code
which tells both of the number selected and winning number.

Asked By: RAKSHIT KHATRI

||

Answers:

If you can use the package random then

from random import randint

possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e']
winning_ticket = ""
print("Winning ticket is...")

for x in range(4):
    winning_ticket += str(possibilities[randint(0, len(possibilities)-1)])
    
print(f"Any ticket matching {winning_ticket} wins a prize!")
Answered By: 132ads

You can use the choice function from Pythons inbuilt random library.

from random import choice

possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e']

# choose four items from the possibilities
chosen = [str(choice(possibilities)) for _ in range(4)]

# print the four chosen items
print(f"Any ticket matching {''.join(chosen)} wins a prize!")

This code makes an array of the four pulled numbers or letter, and then print the entire array at the end.

Answered By: Jack Morgan
from random import choice
lottery = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd');

win = [];

for value in range(4):
    choose = choice(lottery);
    win.append(choose);

print(f"Any ticket matching {win} wins a prize")
Answered By: Hao

Here is another way of doing it random.choices() and NOT .choice().
choices() has a built-in optional parameter that allows me to select the length of the returned list:

— example:—

random.choices(possibilities, k=4) # will return 4 random selection in a list

Also, I wanted the final version not in a list format:
not this: [5, ‘c’, ‘b’, 1]
but this: 5cb1

So, I used join with no delimiter, but i had to convert the returned list to str first as mentioned here.

here is the full code:

Version 1: return only 4 random characters

from random import choices


class WinningTicket:
    """Randomly select characters and compose tkt number."""

    def __int__(self, selection):
        """Initializing attributes."""
        self.selection = selection

    def generate_tkt(self):
        """Randomly select a series of characters to generate tkt num."""
        series = ["a", "b", "c", "d", "e", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.selection = choices(series, k=4)
        return "".join(map(str, self.selection))


tkt_num = WinningTicket()
print(f"nWinning ticket number is: {tkt_num.generate_tkt()}n")

Result:

Winning ticket number is: d881

If you want to take it a step further where you’ll have the option to specify the length of characters for the tkt#:
Just add a parameter to the generate_tkt() method and use it for your k parameter in the returned list:

def generate_tkt(self, length):
    self.selection = choices(series, k=length)

Version 2: return a random characters based on a length value when calling the method

from random import choices


class WinningTicket:
    """Randomly select characters and compose tkt number."""

    def __int__(self, selection):
        """Initializing attributes."""
        self.selection = selection

    def generate_tkt(self, length):
        """Randomly select a series of characters to generate tkt num."""
        series = ["a", "b", "c", "d", "e", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.selection = choices(series, k=length)
        final = "".join(map(str, self.selection))
        return final

tkt_num = WinningTicket()
print(f"nWinning ticket number is: {tkt_num.generate_tkt(8)}n")

Results:
8 characters tkt#:

Winning ticket number is: ae8e4836

5 characters tkt#:

print(f"nWinning ticket number is: {tkt_num.generate_tkt(5)}n")
Winning ticket number is: bd3ac

10 characters tkt#:

print(f"nWinning ticket number is: {tkt_num.generate_tkt(10)}n")
Winning ticket number is: 5b96a3e987
Answered By: snapieee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.