Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 1

Flip a coin \(N\) times. Make a program that simulates flipping a coin \(N\) times. Print out "tail" or "head" for each flip and let the program count the number of heads. (Hint: Use \(\mathrm{r}=\) random random () and define head as \(\mathrm{r} \Leftrightarrow 0.5\) or draw an integer among \(\\{1,2\\}\) with \(r=\) random . randint \((1,2)\) and define head when \(r\) is 1.) Name of program file: flip_coin.py.

Problem 2

Compute a probability. What is the probability of getting a number between \(0.5\) and \(0.6\) when drawing uniformly distributed random numbers from the interval \([0,1)\) ? To answer this question empirically, let a program draw \(N\) such random numbers using Python's standard random module, count how many of them, \(M\), that fall in the interval \((0.5,0.6)\), and compute the probability as \(M / N\). Run the program with the four values \(N=10^{i}\) for \(i=1,2,3,6\). Name of program file: compute_prob.py.

Problem 3

Choose random colors. Suppose we have eight different colors. Make a program that chooses one of these colors at random and writes out the color. Hint: Use a list of color names and use the choice function in the random module to pick a list element. Name of program file: choose_color.py.

Problem 4

Draw balls from a hat. Suppose there are 40 balls in a hat, of which 10 are red, 10 are blue, 10 are yellow, and 10 are purple. What is the prohahility of getting two blue and two purple balls when drawing 10 balls at random from the hat? Name of program file: 4 balls_from10.py.

Problem 5

Probabilities of rolling dice. 1\. You throw a die. What is the probability of getting a \(6 ?\) 2\. You throw a die four times in a row. What is the probability of getting 6 all the times? 3\. Suppose you have thrown the die three times with 6 coming up all times. What is the probability of getting a 6 in the fourth throw? 4\. Suppose you have thrown the die 100 times and experienced a 6 in every throw. What do you think about the probability of getting a 6 in the next throw? First try to solve the questions from a theoretical or common sense point of view. Thereafter, make functions for simulating cases 1,2 , and 3 . Name of program file: rolling_dice.py.

Problem 6

Estimate the probability in a dice game. Make a program for estimating the probability of getting at least one 6 when throwing \(n\) dice. Read \(n\) and the number of experiments from the command line. (To verify the program, you can compare the estimated probability with the exact result \(11 / 36\) when \(n=2\).) Name of program file: one6_ndice.py.

Problem 8

Decide if a dice game is fair. Somebody suggests the following game. You pay 1 unit of money and are allowed to throw four dice. If the sum of the eyes on the dice is less than 9 , you win 10 units of money, otherwise you lose your investment. Should you play this game? Answer the question by making a program that simulates the game. Name of program file: sum9_4dice.py.

Problem 13

Investigate the winning chances of some games. An amusement park offers the following game. A hat contains 20 balls: 5 red, 5 yellow, 3 green, and 7 brown. At a cost of \(2 n\) units of money you can draw \(4 \leq n \leq 10\) balls at random from the hat (without putting them back). Before you are allowed to look at the drawn balls, you must choose one of the following options: 1\. win 60 units of money if you have drawn exactly three red balls 2\. win \(7+5 \sqrt{n}\) units of money if you have drawn at least three brown balls 3\. win \(n^{3}-26\) units of money if you have drawn exactly one yellow ball and one brown ball 4\. win 23 units of money if you have drawn at least one ball of each color For each of the \(4 n\) different types of games you can play, compute the net income (per play) and the probability of winning. Is there any of the games (i.e., any combinations of \(n\) and the options \(1-4\) ) where you will win money in the long run? Name of program file: draw_balls.py. 0

Problem 14

Probabilities of throwing two dice. Make a computer program for throwing two dice a large number of times. Record the sum of the eyes each time and count how many times each of the possibilities for the sum \((2,3, \ldots, 12)\) appear. A dictionary with the sum as key and count as value is convenient here. Divide the counts by the total number of trials such that you get the frequency of each possible sum. Write out the frequencies and compare them with exact probabilities. (To find the exact probabilities, set up all the \(6 \times 6\) possible outcomes of throwing two dice, and then count how many of them that has a sum \(s\) for \(s=2,3, \ldots, 12\).) Name of program file: freq_2dice.py.

Problem 18

Throw dice and compute a small probability. Compute the probability of getting 6 eyes on all dice when rolling 7 dice. Since you need a large number of experiments in this case (see the first paragraph of Chapter 8.3), you can save quite some simulation time by using a vectorized implementation. Name of program file: roll_7dice.py.

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks