Reputation: 3
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()
Upvotes: 0
Views: 421
Reputation: 1314
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:
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.
Upvotes: 1