LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Coin Toss Simulator


@Edjsch wrote:

crossrulz,

 

Yes, I am looking for a streak of 10 heads (or more) from a single coin. Nowhere did it mention that I'm throwing 10 or 1024 coins, or some such number, at once.

In your original post you said "Now here’s the problem: If I check “Continuous” and uncheck it after, say, about 1000 runs (each being 1024 tosses), I usually get the ratio of  “Number of HEADS in a row >= 10” to “Number of Runs” around 40%."

 

So there you first bring up 1024 tosses.

 

But what you say, "What I found with my code is that you will get a streak of 10 or more every ~2048 tosses" seems to disagree with RavensFan's post after yours. He is indicating that counting the transitions from one streak to the next adds to the possibility of increasing the streak (which is true in that narrow sense). So from his reasoning it should be more likely than every ~2048 tosses. He seems to think it will be more frequent than 1/1024.

That doesn't disagree with what I said.  I am talking about the probability of getting at least 1 streak of 10 heads in a run of 2048 is higher than a run of 1024.  It is higher than the sum of the probability of the two individual runs of 1024 (because of the chance a run of 10 can occur on the boundary between the two individual runs.)  Never did I say what the overall chances were of a run of 10 in 1024 or 2048 tosses.

 

Anyway, my code was different than Don's code, as I think was everyone else's. We were trying to count 10 or more heads in a row, and restart the count when a tail came up. We all seem to come up with about 40%. I still can't figure out where that number comes from. I think it should be a binary (i.e., power of 2, i.e., 2^N) number.

 

Can you post your code in LV8.5? Thanks.

 

Ed


 

 

If you really want to test out the the probabilities, take Crossrulz's VI, set N to 1024, then repeatedly run that thousands of times.  Keep a count of how many times a run of 10 heads occurs 1 or more times vs. how many it occurs zero times.  That should lead to the 40% number.

 

Then do the same thing and with N = 2048.  The 40% number will grow.  Set N=10,000, a million, a billion, a trillion.  Plot the % vs. the value of N.  The chart will probably asymptotically go to 100%.

0 Kudos
Message 81 of 92
(1,820 Views)

RavensFan,

 

>> So there you first bring up 1024 tosses.

 

Yes, "tosses", not 1024 individual coins, which might make a difference depending on what we are looking for.

 

I guess I confused the issue by talking about runs of N tosses. crossrulz's vi of simply doing one huge number of tosses is more of what I was after. (Really, I wanted to prove to my co-worker that after a large number of heads in a row the probability of the next toss being heads is still 50%. We all (except him) agree on that. Correct?

 

>> I am talking about the probability of getting at least 1 streak of 10 heads in a run of 2048 is higher than a run of 1024.

 

Yes, of course, when worded like that. (It's all about how the problem is posed.) As the number of "runs" approaches infinity, the probability approaches 100%. Agreed. So, yes, I see that number can be 40% or 53% or 72% as the number of runs grows. It doesn't have to be a power of 2. That's your point. Agreed.

 

Now back to crossrulz's vi. I let it run for 100 million tosses. It showed "streaks of heads >= 10" being 4793, which is every 2086 tosses. That is close to 1/2048, as he said. That is 1/[2^(N+1)] where N = size of the streak, in this case 10. Interesting. But this does show it converges to a power of 2, as I postulated. I originally thought that 10 or more heads in a row should occur on average every 1024 tosses. This vi shows it's every 2048 tosses. I'm off by a factor of 2. (Somewhere I said the probability would be 50%. Maybe that's the factor of 2 I forgot. Smiley Frustrated)

 

[Another interesting fact that I mentioned before is that the sum of all probabilities > 10 is equal to the probability of being equal to 10 exactly. "streaks of exactly 10" (not shown in the vi) = "streaks of heads >= 10" - "streaks of heads > 10" = 4793 - 2393 = 2400. This shows that "streaks of heads > 10" (2393) ~= "streaks of exactly 10" (2400).]

 

Thanks to all for this great exercise!

 

Ed

 

0 Kudos
Message 82 of 92
(1,807 Views)

@Edjsch wrote:

RavensFan,

 

>> So there you first bring up 1024 tosses.

 

Yes, "tosses", not 1024 individual coins, which might make a difference depending on what we are looking for.

 

I guess I confused the issue by talking about runs of N tosses. crossrulz's vi of simply doing one huge number of tosses is more of what I was after. (Really, I wanted to prove to my co-worker that after a large number of heads in a row the probability of the next toss being heads is still 50%. We all (except him) agree on that. Correct?

 

>> I am talking about the probability of getting at least 1 streak of 10 heads in a run of 2048 is higher than a run of 1024.

 

Yes, of course, when worded like that. (It's all about how the problem is posed.) As the number of "runs" approaches infinity, the probability approaches 100%. Agreed. So, yes, I see that number can be 40% or 53% or 72% as the number of runs grows. It doesn't have to be a power of 2. That's your point. Agreed.

 

Now back to crossrulz's vi. I let it run for 100 million tosses. It showed "streaks of heads >= 10" being 4793, which is every 2086 tosses. That is close to 1/2048, as he said. That is 1/[2^(N+1)] where N = size of the streak, in this case 10. Interesting. But this does show it converges to a power of 2, as I postulated. I originally thought that 10 or more heads in a row should occur on average every 1024 tosses. This vi shows it's every 2048 tosses. I'm off by a factor of 2. (Somewhere I said the probability would be 50%. Maybe that's the factor of 2 I forgot. Smiley Frustrated)

 

[Another interesting fact that I mentioned before is that the sum of all probabilities > 10 is equal to the probability of being equal to 10 exactly. "streaks of exactly 10" (not shown in the vi) = "streaks of heads >= 10" - "streaks of heads > 10" = 4793 - 2393 = 2400. This shows that "streaks of heads > 10" (2393) ~= "streaks of exactly 10" (2400).]

 

Thanks to all for this great exercise!

 

Ed

 


 

I'd disagree with CrossRulz's message:

 

"Are you looking for a streak of 10 heads from a single coin or are you looking for 10 coins to all flip heads?  This is a difference.

 

If you are looking for the 10 coins to all be heads, then Don's code is what you are looking for.

 

If you are looking for a streak from 1 coin, then it is flawed.  Don's code counts 10 flips as a single flip.  So the math will be wrong.  What I found with my code is that you will get a streak of 10 or more every ~2048 tosses."

 

Don's code isn't necessarily  a situation of 10 different coins all flipped to heads vs. a single coin flipped 10 times in a row.  It maybe semantics, but code never makes a distinction between one coin vs. several coins.  I'd say Don's code does flip a single coin, and does it 10 times in a row (there is a single random number generator in a For Loop that runs sequentially.)  The problem is that that is then treated as a distinct group that either passes the test of being all heads about 1 in 1024 times, or fails the test.  Practically, it really is no difference from CrossRulz's assertion that this is the same as tossing 10 coins simultaneously and being all heads or not.

 

Then I'd argue that Don's code (when the 1 in 1024 situation of all heads occurs) is flipping a different coin (a separate random number generator in a case structure) to find a heads or tails.  You could argue that this is also the same coin tossed an 11th time.  The code doesn't really care.

 

But this is all about 10 heads in a row of 10 tosses, vs. 10 heads in a row of a much longer string of tosses.  Don's code only functions on a single group of 10.  It isn't designed to go to a larger run of tosses because the AND array function acts on the entire group.  If you tried to use is code with a larger group (such as a 100), it won't find run's of 10 heads because the array function he uses doesn't care about the order any more.

 

Your comment "[Another interesting fact that I mentioned before is that the sum of all probabilities > 10 is equal to the probability of being equal to 10 exactly. "streaks of exactly 10" (not shown in the vi) = "streaks of heads >= 10" - "streaks of heads > 10" = 4793 - 2393 = 2400. This shows that "streaks of heads > 10" (2393) ~= "streaks of exactly 10" (2400).]"  makes sense to me.

 

The distinction between a run of exactly 10 heads in a row vs. greater than 10 heads in a row is based on that 11th toss.  If it is tails, you've now defined a run of exactly 10.  If it is heads, now you have defined a run of greater than 10 heads, no matter how many more consecutive heads you have after that 11th toss.  That 11th toss is 50/50.  And I'd say those results you have pretty much prove the point you were originally trying to prove to this guy that the 11th toss after 10 heads in a row is 50/50 heads vs. tails.

 

0 Kudos
Message 83 of 92
(1,801 Views)

@RavensFan wrote:

I think Norbert got that part wrong where he said the chance didn't change if you added an 11th toss.  When you have a case of getting 10 in a row in 11 throws, it basically gives you a mulligan on the first throw if it happens to turn up wrong.  So 50% of the time you have a 1/512 chance (like you said, you need just 9 more), and 50% of the time you have a 1/1024 chance to get all 10.  I don't remember all the rules of working probability calculations.  But I'm going to guess the end result is 1.5 out of 1024 are your chances with 11 throws.  (Someone with a stronger math/probability background can correct me).[...]



Hehe, you are somehow right and somehow wrong.

It depends on your requirements.

 

If you say:

"I want to have exactly 10 heads in a row in 11 tosses", i am correct. If you state "i want to have at least 10 heads in a row in 11 tosses", you are correct. It is a simple counting of "success results" vs. all possible results.

 

CoinTossTree.PNG

Ok, lets take a close look in that tree graph. It is a toin coss tree with three tosses, each chance (H:T) shall be 50:50. So what is the chance to get HHH?

The algorithm is 0.5^3 = 12.5%. But let's count it out. HHH is only one time present in the last toss-column (top circle). Overall, the tree has 8 circles in the last column, so the chance of HHH could also be stated as "one success out of eight possible results". 1/8 = 12.5%.

Fine, let's look into the next task: What is the chance of getting at least two heads in a row in three tosses? Our pattern we look for is "HH" in three letters. Let's count: Three circles in the last column contain the string "HH":

HHH

HHT

THH

So we have three successes out of eight possible results. This makes up for 3/8 = 37.5%

If we'd ask "what is the chance of getting exactly two heads in a row in three tosses?" instead, the chance would be less. Only two circles cover that requirement:

HHT

THH

The chance would be 2/8 for this = 25%.

 

If would now ask "what is the chance of getting at least two heads (in any order) out of three tosses?", the chance would be different as well:

HHH

HHT

HTH

THH

So our chance now is 4/8 = 50%.

If the question would be "What is the chance of getting exactly two heads (in any order) in three tosses?", chance would be less:

HHT

HTH

THH

Chance is 3/8 = 37.5%.

 

As you can see, we have two keywords in our question which have great impact on chances:

a) at least vs. exactly

b) in a row vs. in any order

You do have the question initially:

"What is the chance of getting at least 10 heads in a row out of 1024 tosses?"

As we can see in my examples above, at least increases chance if the number of tosses is (much) greater than the number of expected "successes". In a row on the other hand greatly decreases the chance. The bad thing is that they do not compensate one another. "In a row" creates dependencies in the results (not affecting the individual toss chance!).

 

Hope this helps shedding light on the discussion,

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 84 of 92
(1,783 Views)

Hm, i did forget to state the initial hook up for my examples above.... 😞

The situation you were confused about with my statement regarding 10 heads in 11 tosses is the "at least" vs. "exactly" confusion. Looking in the tree again, we will now ask two new questions:

What is the chance of getting HH in two tosses?

Answer: 1/4 = 25%

What is the chance of getting exactly two heads in a row in three tosses?

Answer is already in previous post: 2/8 = 25%

So adding a toss does not change the chance if you require exactly the number of successes.

 

If you change the requirement to "at least", everyone else is right that adding a new toss increases the chance.

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 85 of 92
(1,772 Views)

Yes, that's all very interesting, and proves the obvious that how the question is worded changes the results! Smiley Happy

 

Anyway, when I run my vi, attached, say 1 run of 1 million tosses, this is my results (results are similar every time):

 

Coin Toss.PNG

 

Notice that I'm getting my expected results. I get >= 10 heads in a row on average about every 1024 times, as I originally postulated. This agrees with the results I get when I run Don_Phillips' "New HTs.vi". It does check for 10 heads in a row, but runs continuously, so this is also what I'm looking for. Smiley Very Happy

 

I'm not getting 2048 as crossrulz's vi shows.

 

(That 40% number was a red herring; it indicated something else as was pointed out!)

 

(I know my vi is not coded very efficiently, but it works.)

 

Ed

0 Kudos
Message 86 of 92
(1,752 Views)

If you have a run of 11 in a row, is that considered a single toss of greater than/equal to 10 or as two tosses of greater than/equal to 10?

0 Kudos
Message 87 of 92
(1,746 Views)

I think I found a bug in your code.  For your Head>=10 variable, it increments whenever the current streak is >=10.  This means it increments when the streak is at 10, 11, 12, 13, etc.  So for a single streak of 19, it increments 10 times.


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 88 of 92
(1,744 Views)

@RavensFan wrote:

If you have a run of 11 in a row, is that considered a single toss of greater than/equal to 10 or as two tosses of greater than/equal to 10?


The former, I think. But as crossrulz points out, maybe it's the latter.

0 Kudos
Message 89 of 92
(1,738 Views)

@crossrulz wrote:

I think I found a bug in your code.  For your Head>=10 variable, it increments whenever the current streak is >=10.  This means it increments when the streak is at 10, 11, 12, 13, etc.  So for a single streak of 19, it increments 10 times.


Yes, that's how I intended it to work. That looks like the difference with your code, and why mine works like Don's.

0 Kudos
Message 90 of 92
(1,736 Views)