computer science
Homework 9
Suggested due date: November 9, 2021, Last day to turn in without penalty: November 14
For a Google Colab starter file, go to https://colab.research.google.com/drive/ 1ppOBfi8xoeyZLgEg2Wxs9h3mbe_Lslll.
1. Write a program to find the approximate probability of getting an even sum when rolling two standard dice.
2. Let nums = np.arange(10), the integers from 0 to 9 (inclusive). Write a program to find the approximate probability of getting two even numbers if you draw two numbers from this list (without replacement).
One way to approach this: define a variable both even = 0 and total trials = 100. Then use a for loop over total trials and “np.random.choice” to draw two numbers from the list. Then check whether the two numbers are both even. If they are, add 1 to both even. After the loop, the approximate probability is just both even/total trials. If you increase total trials, by the law of large numbers, you get closer to the true probability.
3. Write code to determine the approximate probability that among n = 20 people, two of them have the same birthday (not necessarily the same year). You may assume that nobody was born on February 29.
Hint: You can use the np.random.choice or np.random.randint functions to choose numbers from 1 through 365. In this case, you should NOT use replace = False be- cause each person is an independent choice. You can then check the number of unique elements of your list “birthdays” in this way: len(set(birthdays)). This converts the list to a set, which discards all redundancies, and then counts the length of the set. If this length matches the original length n, then there are no birthdays in common. Repeat this procedure over many trials.
4. Plot the function y = sin x in x � [0, 4π] in python. Add a title with the name of the function to the plot, and label the x and y axes with “x” and “y”.
5. Simulate a normal distribution with mean µ = 5 and standard deviation σ = 5 with n = 100 samples. Then make a histogram of the resulting distribution.
1
6. In a survey of Americans about their favorite food, 21% chose pizza, 16% chose steak, 11% chose tacos, 11% chose pasta, and 13% chose hamburgers, and the rest chose some- thing else (you can label this category as “Other”). Make a pie chart that illustrates these food preferences, and label each slice of the pie, and also label the percentage each captured. Add an appropriate title.
2