Program_Example

Program_Example - A Programming Example Flips Write a...

This preview shows pages 1–7. 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)
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/28/2011 for the course CHE 210 taught by Professor Swinnea during the Fall '08 term at University of Texas.

Page1 / 11

Program_Example - A Programming Example Flips Write a...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online