0

I am trying to create a lottery loop which stops when random generated numbers match the winner ones. But I'm getting this after some time.

---------- FINISHED ----------

exit code: -1073741571 status: 1

import sys
import random
sys.setrecursionlimit(1500000)

lotteryWinner = []
lotteryRandom = []

for i in range(6):
    number = random.randint(1,50)
    while number in lotteryWinner:
        number = random.randint(1,50)
    
   lotteryWinner.append(number)
   lotteryWinner.sort()
print(lotteryWinner)

def main():
    if len(lotteryRandom)>5:
            lotteryRandom.clear()

    for i in range(6):
        number = random.randint(1,50)
        while number in lotteryRandom:
            number = random.randint(1,50)
        lotteryRandom.append(number)
        lotteryRandom.sort() 
  
    print(lotteryRandom)  

    if lotteryWinner != lotteryRandom:
        main()
        

    if lotteryWinner == lotteryRandom:
        print('You win')     
main()
Community
  • 1
  • 1
retardo
  • 3
  • 3
  • Whats the desired output? – U13-Forward Jan 15 '20 at 04:01
  • Check this https://stackoverflow.com/questions/20629027/process-finished-with-exit-code-1073741571 . – Kiran Indukuri Jan 15 '20 at 04:06
  • 1
    Could you explain a bit more? are you randomly generating both lotteryWinner and lotteryRandom arrays? or is it like, you want to rondomly create the lotteryRandom array, manually input a list of arrays and pick the winner from there which matches with the lotteryRandom array? – Subhashi Jan 15 '20 at 04:18
  • I wanted to generate lotteryWinner once and then generate lotteryRandom untill they match – retardo Jan 15 '20 at 04:31

1 Answers1

1

The exit code you receive occurs to indicate that the program indeed did recurse till the provided limit, however reaches the maximum number of recursions. Often it is also necessary to provide the threadinglimit. But since the program is based on random number generation, it might just work some time when a matching list is indeed found. However, it is important to understand how small a probability you are dealing with. You are trying to match 5 random numbers in the two lists which includes:

  1. You want to generate the exact same 5 numbers.(from 1 to 50, where the chaces of picking 1 number is 1/50 and the probability of picking 5 numbers is (1/5)^5)
  2. You want them in the same order in the list. (sorting them as you have done is a good choice)

To make the chances better, one of the multiple things you could do is

import sys
import random
sys.setrecursionlimit(1500000)

lotteryWinner = []
lotteryRandom = []

for i in range(6):
    number = random.randint(1,10)
    lotteryWinner.append(number)
    lotteryWinner.sort()
print(lotteryWinner)

def main():

    number = random.randint(1,10)

    if number not in lotteryWinner:
        main()


    else:
        print('You win')     
main()

Output:

[3, 4, 6, 9, 10, 10]                                                                                                                                           
You win 

To improve the chances, the range for random integer generation has been reduced and the program only checks if the generated number is in the initial list of generated numbers. You could increase the range for random number generation to make winning more rare.

Vishakha Lall
  • 1,178
  • 12
  • 33
  • I see, I thought I could wait for hours to find perfect match while burning my cpu :) – retardo Jan 15 '20 at 04:26
  • There are several other possibilities you could think of, without burning your CPU. :) Also, please upvote/mark the answer if you found it helpful. – Vishakha Lall Jan 15 '20 at 04:32