Python: Average number of drawings (Randomly generated numbers)

Question:

I’m very new to Python and I hope for some help or guides by asking here.

Here’s the problem:
Write a program that estimates the average number of drawings it takes before the user’s numbers are picked in a lottery that consists of correctly picking six different numbers that are between 1 and 10. To do this, run a loop 1000 times that randomly generates a set of user numbers and simulates drawings until the user’s numbers are drawn. Find the average number of drawings needed over the 1000 times the loop runs.

I tried to create something (below), but I just can’t think of how to get those average number. Also it seems the loop is not good. Any help or solution? thank you in advance.

from random import randint
from random import choice            #???

userlist = []
for y in range(6):
        user = input("Enter your entry no.{} lotto number: ".format(y+1))
        userlist.append(user)

x = 0
randomlotterylist = []
while not x>1000:
        lottery = []
        for i in range (6):
                lot.append(randint(1,10))
        randomlotterylist.append(lottery)
        x = x + 1

#Next.. ????
Asked By: arcwinolivirus

||

Answers:

First, you want to know your theoretical average number of drawings, that’s (1/10)^6 assuming no repetition allowed. Therefore on average every 1,000,000 tries you’d hit the correct number. That is only if the order matters but I assume in your case the order does not matter, so def your average is less than that…

from random import randint

def number_of_tries_before_hitting_jackpot(user_number):
    n_tries = 0
    while True:
        random_number = set([randint(1,10) for i in range(1,7)])
        if user_number == random_number:
              return n_tries
        else:
              n_tries+=1

def ask_user_his_number():
   userlist = []
   for y in range(6):
        user = input("Enter your entry no.{} lotto number: ".format(y+1))
        userlist.append(user)
   return set(userlist)

n_tries_list = []
for x in range(1,1001):
       user_number =  ask_user_his_number()
       print user_number
       tmp = number_of_tries_before_hitting_jackpot(user_number)
       print tmp
       n_tries_list.append(tmp)
avg_number_drawings = reduce(lambda x, y: x + y, n_tries_list) / len(n_tries_list)
print avg_number_drawings

This code is not what I’d do in the sense that the user needs to input its 6 numbers 1,000 times (v annoying for the user). You could change the ask_user_his_number function to a function that just randomly selects a set of 6 numbers.

Answered By: Dnaiel

by using random module, for loop, list in short without defining fuction.

from random import *  

user_num=[]
count=0
for i in range(6):
     random_num=randint(1,10)
     user_num+=[random_num]
     user_num.sort()

for j in range(1000):
     picked=[]
     for k in range(6):
          pick_num=randint(1,10)
          picked+=[pick_num]
          picked.sort()
     if picked==user_num:
          count+=1
print("the avg darwing is: ",count, count/1000)
Answered By: zim
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.