Created
April 27, 2011 18:53
-
-
Save anonymous/944924 to your computer and use it in GitHub Desktop.
How many heads/tails in a row can you get if you keep flipping?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% FlipCoin experiment - how many os/1s in a row you get on average? | |
clear; | |
%% Declare some constasts | |
ITERATIONS = 100000000; | |
results = zeros(ITERATIONS, 1); | |
%% Main loop | |
for i=1:ITERATIONS, | |
% some variables | |
firstoutcome = -1; | |
howManyInARow = 0; | |
stopFlag = 0; | |
% flipcoins and count how many 1s in a row | |
while(stopFlag ~= 1) | |
% flip an imaginary "binary coin" | |
flipcoin = randi([0 1]); | |
% set first outcome | |
if(firstoutcome == -1) | |
firstoutcome = flipcoin; | |
continue; | |
end | |
if(flipcoin == firstoutcome) | |
% if it's a 1 increase counter | |
howManyInARow = howManyInARow + 1; | |
else | |
% if it's a 0 set the stop flag | |
stopFlag = 1; | |
end | |
end | |
results(i) = howManyInARow; | |
% give some feedback once in a while | |
if(rem(i,ITERATIONS/10) == 0) | |
fprintf('current iteration:%d\n', i); | |
end | |
end | |
%% Calculate average | |
Average = sum(results)/ITERATIONS; | |
fprintf('Average of repetitions of the same outcome in a row: %d\n', Average); | |
fprintf('Max sequential occurrences of the same outcome in a row: %d\n', max(results)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment