Question
(d) We've said that software models the real world. One aspect of the real world that we sometimes want to model is randomness. If we're
(d) We've said that software models the real world. One aspect of the real world that we sometimes want to model is randomness. If we're writing software to play a card game, we don't want the same cards to come up every time we play. If we're designing a simulation (let's say of the flow of customers through a supermarket, to determine how many checkers we need on duty), we don't want one customer to arrive like clockwork every 30 seconds; we want a more realistic and less predictable flow of traffic.
We achieve randomness in software using a random number generator. Python has a library of functions dealing with randomness; it's called random. The randrange function generates random integers in a specified range, so that randrange(5) returns a number that's either 0, 1, 2, 3, or 4 (like range() in Python, randrange() refers to numbers starting at zero and going up to, but not including, its argument). When given two arguments, the randrange function uses the first as the lower bound of the range, so randrange(1,5) returns a number that's either 1, 2, 3, or 4.
(d.1) Start with the statement from random import randrange. Then write a simple for-loop that prints out 50 random numbers between 0 and 10 (inclusiveso you want some 10s in your results). Next, write a simple for-loop that prints out 50 random numbers between 1 and 6 inclusive (like rolling standard six-sided dice). Check your results carefully: With 50 numbers, you should have a few of each possibility; if one value (especially the first or the last) doesn't show up at all, your code isn't correct.
(d.2) Many dice games roll two six-sided dice at once, resulting in totals ranging from 2 (sometimes called "snake eyes" because of the single dot on each of the dice) to 12 (sometimes called "boxcars" after the rectagular cars on freight trains).
Write a function called roll2dice that takes no parameters and returns a number that reflects the random roll of two dice. [Hint: It's not quite as simple as returning one random number between 2 and 12 inclusive. With two real dice, some results occur more frequently than others.] Test your function by calling it 50 times in a for-loop, printing the result each time. (You'll note that precise testing of code involving randomness is hard, because the whole point is that you have unpredictable data each time. But the next part of this problem shows one approach to this problem.)
(d.3) Write a function called distribution_of_rolls that takes one numberthe number of times to roll two diceand prints the distribution of the values of those rolls in the form shown below. The output below is a possible output from distribution_of_rolls(200).
Distribution of dice rolls
2: 7 ( 3.5%) *******
3: 14 ( 7.0%) **************
4: 15 ( 7.5%) ***************
5: 19 ( 9.5%) *******************
6: 24 (12.0%) ************************
7: 35 (17.5%) ***********************************
8: 24 (12.0%) ************************
9: 28 (14.0%) ****************************
10: 18 ( 9.0%) ******************
11: 9 ( 4.5%) *********
12: 7 ( 3.5%) *******
-----------------------------
200 rolls
Here are some details: We don't have time to discuss output formatting in class; you can pick up details you need in the textbook(s) and especially in the String Formatting document (Links to an external site.)Links to an external site.. You'll need a list of numbers for counting each of the possible rolls; think about an easy way to get to the tally for roll n (it's not good to have 11 separate variables, count2s, count3s, count4s, and so on). The histogram above shows 200 rolls; if you wanted to try it with much larger numbers, you could scale the histogram differently so that one star counts for, say, 20 rolls.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started