{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Program_Example

# Program_Example - A Programming Example Flips Write a...

This preview shows pages 1–11. Sign up to view the full content.

A Programming Example

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flips Write a program that simulates n flips of a coin. The program is to keep track of the number of head and tails outcomes. It is to ouput the number of flips, the number of heads and tails, and the percentage of flips that results in a heads outcome.
Things to think about What programming structures are required? How do you simulate a flip? How will you keep track of each outcome?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The rand Function rand – produces a single pseudo-random number between 1 and 0. Each use of rand produces a new value in a series of random numbers. The first call to rand after starting MATLAB will always produce the same first number and thus the same series of numbers. The random number generator can be made to start with a different first number by first executing the command: rand('state',sum(100*clock)) How can this be used to produce a head or tail?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
n=input('How many flips? '); heads = 0 ; tails = 0 ; for i = 1:n flip = round(rand); if (flip == 0) heads = heads + 1; elseif (flip == 1) tails = tails + 1; else error( 'bad flip' ); end end fprintf('\nAfter %i flips',n); fprintf('\nThere are %i heads and %i tails',heads,tails);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}