1

I am wondering how I can get 6 unique random numbers. I have read about random.sample but in my case, I am generating 5 numbers 1-69 and the 6th number 1-26. I need all 6 to be unique. This is my code.

import tkinter
import random

class Lottery_GUI:
    def __init__(self):
        self.main_window = tkinter.Tk()
        self.main_window.geometry('500x100')
        self.main_window.title('Lottery Number Generator')
        self.frame1 = tkinter.Frame(self.main_window)
        self.frame2 = tkinter.Frame(self.main_window)

        self.frame1.pack()
        self.frame2.pack()

        self.list1 = tkinter.IntVar()

        #Lottery label
        self.Lottery_label = tkinter.Label(self.frame1,\
                        text = 'Welcome to the Lottery Generator')

        self.Lottery_label.pack()

        #Button to generate the numbers
        self.Lottery_button = tkinter.Button(self.frame2,\
                        text = 'Click to Generate Numbers',\
                        command = self.Generate_Num)

        self.Lottery_button.pack()

        #Display the random numbers

        self.num_entry = tkinter.Entry(self.frame1, textvariable = self.list1,\
                    width = 20, fg = 'blue', justify = 'center')

        self.num_entry.pack(side = 'bottom')

        tkinter.mainloop()

    def Generate_Num(self):
        a = random.randint(1,69)

        b = random.randint(1,69)

        c = random.randint(1,69)

        d = random.randint(1,69)

        e = random.randint(1,69)

        f = random.randint(1,26)

        list = [a,b,c,d,e,f]
        list.sort()
        self.list1.set(list)

my_lottery = Lottery_GUI()

I have tried doing random.sample(range(1,70, 5) in the function Generate_Num, but the problem is I can't check the 6th number which only ranges from 1-26.

gmds
  • 19,325
  • 4
  • 32
  • 58
Sleepy
  • 41
  • 5
  • 1
    Possible duplicate of [How do I create a list of random numbers without duplicates](https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates) – benvc Apr 14 '19 at 01:37
  • @benvc I don't think this is a duplicate, because not all the random numbers are sampled from the same space. – gmds Apr 14 '19 at 01:38
  • @gmds - fair point even if it is a minor variation. Close vote retracted. – benvc Apr 14 '19 at 01:40

2 Answers2

2

You can sample without replacement. See np.random.choice

This should work:

a = np.random.choice(26, 1)
np.random.choice(np.delete(np.arange(69), a),5, replace=False)

a is your 6th element.

dgumo
  • 1,838
  • 1
  • 14
  • 18
2

Instead of using randint, use sample and choice with filtering:

import random

first_five = random.sample(range(70), 5)
last = random.choice([number for number in range(27) if number not in first_five])

numbers = first_five + [last]
print(numbers)

Output:

[53, 52, 9, 0, 8, 21]

You can perform sorting and other stuff later.

Also, avoid naming your variable list as that shadows (hides) the builtin.

gmds
  • 19,325
  • 4
  • 32
  • 58