# Lottery Simulation

Plays the lottery
AttachmentSize
lotto_sim.pde1.41 KB

This project is pretty simple. It is a atmega 168 with the arduino bootloader running a lottery code i wrote. The lottery code has 6 user chosen lottery numbers in it. It uses the TrueRandom library to get true random numbers. The lottery in Florida, USA. Draws 6 numbers randomly in between 1 and 53. The program does the same. It comes up with 6 random numbers in between 1 and 53. It then checks to see if these match the users lottery numbers. If it does, then it lights the LED, if it doesnt then it goes back through the loop. The program makes sure not to match the same number more than once when it is checking the 6 numbers. The odds of winning this lottery is 1 in 20,358,520. My lotto numbers i am using are 7, 14, 17, 25, 32, 44.The program runs around 100,000 times a second. I have yet to see the light come on and do not know if i ever will :) and this is why i can not really shot a video of it. I will leave a comment if it ever comes on meaning that my program "won the lottery".

## Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

All i need to do to cut down on time is build more.

a probability of randomly selecting specific number between 1 and 53 is 1/53.

a probability of randomly selecting two specific numbers between 1 and 53 is 1/53 x 1/53.

So, for six numbers it is (1/53)^6 .. i.e. to the sixth power.

(1/53)^6 = 4.5117479E-11 which is quite a lot smaller than 1/20358520 (=4.9119485E-08)

Recalculating the time required for a successful draw like I did before, using a probability of 4.5117479E-11, rather than 4.9119485E-08 gives:

1/(4.5117479E-11*10000) = 25 days - i.e. just under a month.

I don't think your led is going to turn on any time soon!

I believe how our lottery works is there are 53 balls with numbers 1-53, but thats it, once you draw a ball your chances are now 1/52 and then draw another ball takes that out of play so your chances come to 1/51. You can not have lottery numbers like 7 7 6 15 15 20, you do not repeat numbers in the big lottery, i think. Can some one tell me if this is correct? i am not old enough to play the lottery so i wouldnt know :P. Oh and like i said before, the program doesnt run at 100,000 times a second, that is some what off. But even if it did, that is not how many times it "plays the lottery" it takes dozens of clock cycles for the program to run through the library and generate the 6 random numbers. So it is playing the lottery way less than you think so it will take a lot longer to light up.

Oh right, well if your sample size decreases by 1 each time a ball is chosen, then from an original set of 53 numbers, the probability goes like:

(1/53)x(1/52)x(1/51)... and so on. which for six numbers would give you a probability of one in 16529385600

The other question is if the  ORDER of the numbers is important. If it's not, then the probaility goes like:

(6/53)x(5/52)x(4/51).. etc. which, for six numbers, would give you probability of one in 22957480

Does your code check for the order of the numbers?

The order doesnt matter for our lottery.

The order doesnt matter for our lottery.

"....The program makes sure not to match the same number more than once when it is checking the 6 numbers. The odds of winning this lottery is 1 in 20,358,520. My lotto numbers i am using are 7, 14, 17, 25, 32, 44.The program runs around 100,000 times a second..."

so, if I'm right, it should take this many seconds to find a match:

probpertest=1/20358520

tests_persec=100000

prob_persec=probpertest*test_persec=4.9119483E-04

so the number of seconds required for a probability=1 is

1/prob_persec=2035.852 seconds

=33 minutes.

so if your probability estimates are correct, it should only take about half an hour till the led comes on?

This is one very elaborate way to make an LED blink! And that's what I love about this thing.

The blink is gonna be very short though. At this rate, it will blink once every 200 seconds for .001 millisecond. At an average. You could also see this as a PWM'ed LED with a duty cycle of 1:20 million and a very variable frequency.

In other words, you might as well unplug the Arduino and your audience would see the same thing. How about an LED that lights up and dies out over the next 100000 loops, using true PWM? That would introduce some level of persistence in the system.

The LED will not blink if it wins the lottery, the LED will stay lit until the arduino is turned off. That is because i would not be around to see a blink, but if i walk passed it and see that the LED is on, then i know that sometime ago it won the lottery. The night i made this, Telefox and I did some calculations in the shout box and determined that this thing will take a few weeks on average of running time to light up. Thats because the program runs much slower than i thought, it takes a few cycles for the library to come up with the random values and on top of that it takes the program 6 cycles to come up with 6 random numbers (this is how i wrote the program). So you would need the LED to light up and die out over a period of a few weeks now try and do that :). Why would we want persistence? As a viewer watching this simulation, there is always the same odds that it will light up so you may get lucky but you need the same luck that playing that lottery has :P. Being that it is random on start up and shut down, i am able to turn it off and it continues to be random when i turn it back on. Maybe what might be more interesting would be keeping the LED on and then turning it off when it wins the lottery. So basically you would have a LED night light that most likely would stay on unless it wins the Lottery.

Excuse me for trying to learn Arduino C without owning one. I may be wrong all along, but you are capable to explain this like nobody else.

I understand you designed the code to keep the LED on until you reset the Arduino. That makes sense for the reasons we both mentioned.

But reading the code, I cannot see the LED staying on. For the LED pin to go high, variable "match" must be 6. And at the end of every six draws (line 85), that var is reset to 0. Isn't it? And the LED pin is reset to low at the start of every loop (line 31).

If the code must remember that you won the lottery at least once since boot up, you probably need an extra var. Something like "iwoniwoniwon". And make your LED status depend on that one.

Or stop resetting the pin altogether.

Please don't take my nitpicking as criticism. I am a student of this stuff, just like you are.