+ 3

Challenge: Lottery Simulator

Here is a little challenge: write a code in any language that simulates a lottery draw with 4 out of 49. Place a bet, run the simulator on the bet and count how many times you would have to play to win the first time. For 4 out of 49 the chance is 1:211876 (give or take). Considering a payout of $1000 for a win and a price per try of $10, was it worth it? I tried 6 out 49. Took my Macbook Pro 5 min to simulate x) . Sololearn will likely give you a timeout. My version (as Python beginner): https://code.sololearn.com/cHsfH7tmtIU4/?ref=app#py comments to improve the code above welcome

29th Jan 2018, 9:02 PM
Marco Polidori
Marco Polidori - avatar
3 Respostas
+ 8
Just asking: I know you want a simulation of the real thing, but since you have the odds calculated (1:211876), would it be more practical to select a number within the range 1 to 211876, and then let the program pull a random integer value from the range and compare it to your selected value? With a few steps taken to calculate the odds, while it doesn't affect much, it might save a number of iterations.
29th Jan 2018, 9:18 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
The result would be very similar indeed. But I think you could theoretically take more than 211876 times to win one. Also I wanted to know the computing power for simulating ;).
29th Jan 2018, 9:22 PM
Marco Polidori
Marco Polidori - avatar
+ 1
I wrote this to prove to my mother that she should save her money. in my area we have a lottery she plays called lotto 47.. It draws 6 numbers between 1 and 47. It runs a few million times before matching all 6. import random nums=[] run=True lotto=[6,13,26,33,37,42] count=0 win=False while win==False: while run: x=random.randint(1,48) if x in nums: continue else: nums.append(x) if len(nums)==6: run=False nums.sort() count+=1 print("\n") print(lotto) print(nums) if nums == lotto: print ("Winner! It took {} tries".format(count)) win=True nums=[] run=True
29th Jan 2018, 10:33 PM
LordHill
LordHill - avatar