Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

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 ]
ImageAnalyst

Posts: 12,932
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);
uiwait(helpdlg(message));



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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.