LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Coin Toss Simulator

I have been reviewing the code snips and I can see some differences in the implementation so I think we might need some clarification.

In my code (and I think your original code) I am checking each block of 1024 of randoms for a streak of 10 or more.

If I am reading 

0 Kudos
Message 21 of 92
(1,382 Views)

@Don_Phillips wrote:


I counted that as 4.  For probability reasons, we should keep track of all of the streaks that were found during each 1024 runs, sum them up, and divide by the total number of runs of 1024.  When I did 1000 runs of 1024, I kept getting between 425 and 525 streaks of 10 or more.  Repeated runs show that these will average out as approaching inifinity.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 22 of 92
(1,378 Views)

crossrulz,

 

I concur. That was my intent, too, to count them as 4. Maybe I wasn't, which is why I'm only getting 40%. Say there are 4 streaks of 10. On the next toss, half will continue the streak and half won't. So on avarage, where there are N number of streaks of ten or more, there will be N/2 streaks of 11 or more, etc.

 

You said, "I kept getting between 425 and 525 streaks of 10 or more". That averages out to 475. Shouldn't it average 500?

 

Ed

0 Kudos
Message 23 of 92
(1,371 Views)

Also, maybe I (we) should try a single "toss" of say 1 million, and see how many streaks of 10 or more there are.

0 Kudos
Message 24 of 92
(1,367 Views)

Edjsch wrote:You said, "I kept getting between 425 and 525 streaks of 10 or more". That averages out to 475. Shouldn't it average 500?

I was just eyeballing.  My numbers weren't exact and I probably typed wrong.  But I was seeing a bouncing around 500 with each run.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 25 of 92
(1,352 Views)

I am not sure what in the Wide, Wide World of Sports is going on around here.  Sketchy LV checking sketchy math. 

 

Let's make the math simple, then you can check your LV code against it (or vice versa Smiley Wink )

 

Consider the expected number of tosses before a run on length n heads is encountered.  For n>5 then the expected value is very close to 2^(n+1).  It is easy to work out the pattern to be <#tosses> = 2*(2^n-1) if you choose.  The easiest part is the simple intuition that the expected number for n+1 is twice that for n.  Start with the simple n=1 case and you get the right answer for not-small n.

 

For n=10, say, the expected number of tosses is roughly 2^(11) = 2048 to achieve a run of 10 heads.  Now you are asking a very simple question:  If you are drawing on a distribution with a known mean for a given fraction of the mean, what is the cumulative probablility?  The PDF is a simple chi-squared distribution with two degrees of freedom.   The normalized variable is #tosses/2^n, and for this case (2 degrees of freedom), the CDF is simply given by 1-e^(-tosses/2^(n+1)).  (The factors of two come and go based on the definition of Chi-Squared).  You can also use the Chi-Squared CDF VI in the mathematics palette.

 

I am on my phone so this should all be double checked by your LV, but the test is simple:

 

For 1024 tosses I say that the probability of a run of at least n=10 heads is given by 1-e-(1024/2048) = 0.3934.

For 2048 tosses (the expected number to achieve 1 run of ten heads), the probability is 1-e^-1 = 0.6321.

 

Should scale as well, for 32 tosses the probablility for n=5 is 0.3934.

 

Message 26 of 92
(1,349 Views)

Darin,

 

Hmmm, this is turning into quite the mathematical debate.  I added to my code to look to see how many of the 1000 iterations had a run of at least 10 and I also got around the 39%.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 27 of 92
(1,320 Views)

Wolfram alpha seems to agree with ~39%. (Somebody more familiar with the syntax can probably get a more specific answer;) ).

 

 

0 Kudos
Message 28 of 92
(1,306 Views)

Hi Altenbach,

 

That can’t be right. Do it for 4 tosses. The probability of getting a given number of heads in a row (N) is 1/(2^N). Try it yourself for one toss (it is 1/2^1 = 1/2), then 2 (it is 1/2^2 = 1/4), and so on. Therefore the probability of getting 4 in a row is 1/16. So to get 4 heads in a row you have to make 16 tosses, on average. Work it out on a piece of paper in a binary tree. After each toss the probability of the next toss being heads is 1/2. To get 10 in a row you must make 1024 tosses on average. That means, in 1024 tosses the probability of getting 10 in a row is 1/2 (or 50-50 or 50%). My program proves that. In 1024 tosses about half of them have at least 1 run of >= 10 in a row.

 

I tried what I said yesterday. I altered my program to simply count the number of HEADS >= 10. I also changed my “Number of coin tosses to make” to 2^20 = 1048576 and make one run at a time (Continuous is unchecked). I got 1104 heads in a row >= 10 in the first run. (I click “Reset” between each run.) The next run gave 1011. The third 1134. The fourth 1019. And the fifth 1069. The average is 1067. That is what I expected because the probability of getting 10 heads in a row is 1 in 1,024. 1067 is close to 1024.

                                                              

The probability of getting 11 in a row is half that (1/2048). The probability of getting 12 in a row is half that again, and so on. So the probability of getting >= 10 in a row is somewhat greater than 1 in 1024, making what I got, 1067, reasonable. 

 

Ed

0 Kudos
Message 29 of 92
(1,284 Views)

Be careful, there are two different questions going on here.

 

The probability of getting a run of >= 10 heads in 1024 tosses is 40%.  One or two or three or four runs does not matter, the question is whether a run exists or not.

 

The expected number of runs in 1024 tosses is 0.5.  You can not have half of a run so you get a distribution, sometimes zero, sometimes one, sometimes two, and so on. 

 

You proved the expectation value = 0.5, I was considering the probability for determining if a given toss contains a run which = 0.394.

 

 

0 Kudos
Message 30 of 92
(1,275 Views)