Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: Coin Flip Code Help
Posted:
Nov 23, 2013 10:32 AM


Kyle O'Shaughnessy:
If you want to do a Monte Carlo simulation and run it thousands or millions of times, instead of asking the player each and every time, you can have the computer pick for the player. See this little demo:
% Monte Carlo simulation of coin tosses. numberOfTosses = 10000; playerGuesses = logical(randi(2, 1, numberOfTosses)1) actualResult = logical(randi(2, 1, numberOfTosses)1) % See how many times they guessed correctly. % Consider both heads and tails. correctGuesses = (playerGuesses & actualResult)  (~playerGuesses & ~actualResult) numberCorrect = sum(correctGuesses);
% Let's see what the results were: headsGuessed = sum(playerGuesses) tailsGuessed = numberOfTosses  headsGuessed headsActual = sum(actualResult) tailsActual = numberOfTosses  headsActual
message = sprintf('You guessed correctly %d times out of %d = %.2f%%',... numberCorrect, numberOfTosses, 100*numberCorrect/numberOfTosses); uiwait(helpdlg(message));



