From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Coin Toss Simulator

Hi,

 

I wrote a program in LV 8.5 called “Coin Toss Experiment”, attached, which uses the “Random Number (0 to 1)” vi. It is to prove to a disbeliever that if you have a streak of heads or tails, the next toss will have a 50% chance of being a head, and a 50% chance of being a tail. This seems intuitively obvious to most people. But some people get hung up on this: Say there are 10 heads in a row. This person said he would be heavily that the next toss will be a tail. His erroneous reasoning is that the likelihood of there being 11 heads in a row is low, which is true. But that is before ANY tosses have been made. He doesn’t understand that there are ALREADY 10 heads in a row (an initial condition), and each and every toss is an independent event. So the eleventh toss still has a 0.5 probability of being heads. This is why some “gamblers” believe in “streaks” and alter their bets accordingly. (It is also why they are broke!)

 

If you run my program and click the TOSS button, it will make 1024 coin tosses (by default, which can be changed by the user). It will display the average of the random numbers, which will be very close to 0.5, and the total number of HEADS, which is defined as a number from 0.5 inclusive and up to but not including 1. The number of heads will be half the number of tosses, on average. You can try it yourself to prove.

 

I also have an indicator labeled “Max number of HEADS in a row in these tosses”. Probability theory states that the probability of getting a given number, N, of heads (or tails) in a row of a fair coin toss will be 1/(2^N). So in every 1024 (2^10) tosses, the probability of getting 10 heads in a row is 0.5 or “50-50” (an even bet). If you run a number of “TOSSES” you will see that about half the time there will be 10 or more heads in a row. Just make several runs (or check the “Continuous” check box) (uncheck it to stop), and compare the “Number of HEADS in a row >= 10” with the “Number of Runs” displayed.

 

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%. It should be much closer to 50%. For example, I just did 1081 runs and got 444 runs with 10 or more heads in a row, which is 444/1081 = 41%. I ran it again and got 427/1061 = 40%.

 

Can anyone see what might be wrong with my program? I don’t think it’s the random number generator because it is correctly averaging very close to 0.5. I’m suspecting a possible race condition due to my use of local variables, but don’t see it. And I don’t know if there is an alternative way of coding this without using local variables. Shift registers (or feedback nodes) is a possibility, but I don’t see how to do this better and/or more simply without using local variables. Is there a way?

 

Thanks for your advice!

 

Ed

0 Kudos
Message 1 of 92
(4,235 Views)

You did not attach anything.

0 Kudos
Message 2 of 92
(4,223 Views)

I thought I did. Maybe I need to click the link "Add Attachment" after I browse? Here it is again.

 

Thanks,

Ed

0 Kudos
Message 3 of 92
(4,216 Views)

It doesn't seem to be working. I'll close my browser and try again.

0 Kudos
Message 4 of 92
(4,211 Views)

Doesn't seem to help. The first time there was a pop-up (I think from NI) to allow installation of an add on. I said yes. Don't know what the problem is. I've added attachments before without any problems. Maybe I'll try Google Chrome.

 

Ed

0 Kudos
Message 5 of 92
(4,204 Views)

With Google Chrome...

0 Kudos
Message 6 of 92
(4,197 Views)

That worked! I've had several websites with problems with the latest IE (I'm using 😎 that work with Google Chrome! Here in the Attachments box next to the Choose File button, it says either "No file chosen" or my file name. That was not there with IE.

 

Ed

0 Kudos
Message 7 of 92
(4,194 Views)

Edjsch wrote 

And I don’t know if there is an alternative way of coding this without using local variables. Shift registers (or feedback nodes) is a possibility, but I don’t see how to do this better and/or more simply without using local variables. Is there a way?


You need to use shift registers, as you mentioned. That will eliminate potential race conditions. For example, right now you set several indicators (Heads, Num Heads, HEADS in a row) to 0 in parallel with a for loop that writes to local variables of those indicators. You don't know which will execute first - it's possible that one or more iterations of the for loop will execute before the indicators get set to 0, and that could cause problems. Try rewriting it without any local variables (except maybe on the first call for initialization).

0 Kudos
Message 8 of 92
(4,164 Views)

Yes, there is a known bug with attachments in certain versions of IE. See here.

 

There are plenty of race conditions in your code, for example the "heads in row" local variable near the right loop boundary will get read before the code inside the big FALSE case updates it. Your "runs" increment and the zeroing of the three indicators of the left of the loop execute in parallel to the inner FOR loop and there is no guarantee that they are zeroed before something is aready written to them in the loop.

 

To simplify the math, I would also simply keep the sum in the shift register and divide for the average indicator. Keeping the average in the shift register need additional convoluted code as you currently do.

 

 

0 Kudos
Message 9 of 92
(4,156 Views)

Thanks for your suggestion. To test your theory it was easiest to simply put resetting the local variables in the first frame of a Flat Sequence Structure and the For Loop in the second frame. The results were the same, about 40%.

 

(BTW, I'm not sure if this can be done without ANY local variables. Notice that the Case structure in the For Loop has the same locals in both the True and False cases.)

 

With what my program is telling me that 60% of the time TAILS will come up after a streak of 10 or more heads, which we KNOW is not true. Any other suggestions?

 

Ed

0 Kudos
Message 10 of 92
(4,152 Views)