Homework 2 CEN 4010 Spring 2017 A common use of random numbers is to simulate ev
ID: 3811242 • Letter: H
Question
Homework 2 CEN 4010 Spring 2017 A common use of random numbers is to simulate events using a program, rather than going through the time and expense of constructing a real-life experiment. For example, statistical theory tells us that the probability of having a tossed coin turn up heads is 0.5. Similarly, there is a 50 percent probability of having a single tossed coin turn up tails. Using these probabilities we would expect a single coin that is tossed 1000 times to turn up heads 500 times and tails 500 times. In practice, however, this is never exactly realized for a single experiment consisting of 1000 tosses. Instead of actually tossing a coinl000 times, however, we can use a random number generator to simulate these tosses. In particular, we could use any random number generator function that is available in the market or develop our own random number generator function. Let's assume we have developed afunction "rand that generates random numbers between 0 and 0.99. Using this normalized random number, we can simulate coin tosses for number of times until we get the desired output. We can use the following algorithm to develop our program: Initialize the heads count to zero Initialize the tail count to zero For 1000 times get a random number between 0 and 1 ifthe random number is greater than 0.5 consider this as a head and add one to the heads count else consider this as a tailand add one to the tails count end if enf for calculate the percentage of heads as the number of heads divided by 1000x100% calculate the percentage of tails as the number of tails divided by 1000x100% print the percentage of heads and tails obtained Design a software system that generates a random number (or calls random number generator function), then performs coin toss simulation.Explanation / Answer
import random
# heads tails
heads = 0
tails = 0
# running 1000 times
for i in range(1000):
# generating a random number between 0 and 0.99
toss = random.random()
if toss > 0.5 :
heads = heads + 1
else :
tails = tails + 1
# printing heads and tails
print "Heads: ", heads*100/1000.0
print "Tails: ", tails*100/1000.0
# Sample Output
Heads: 48.5
Tails: 51.5
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.