The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Coin Flip Code Help
Replies: 2   Last Post: Nov 23, 2013 10:32 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 13,022
Registered: 12/26/06
Re: Coin Flip Code Help
Posted: Nov 23, 2013 10:32 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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);

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.