Reference no: EM132360733
Question
Probability - Suppose we have a perfect six-sided standard die (the faces have the numbers 1 through 6, one number per side). If we roll the die, what number will come up? That question does not have a simple, correct answer. But a little thought suggests that if the die is tossed lots of times, then, on average, any one of the numbers from 1 to 6 should show up one sixth of the time.
This thinking leads to a definition. The theoretical probability of an event happening is the quotient of the number of outcomes favorable to the event divided by the total possible number of outcomes. When a single die is thrown once, there are six possible outcomes, and exactly one of them is favorable to the event of seeing a 4, say, and so the theoretical probability of seeing a 4 is 1/6. In fact, the theoretical probability of seeing any specific one of the numbers from 1 to 6 is 1/6.
We can also talk about the experimental probability of seeing a 4. This term applies when you actually take a die and toss it lots of times. You divide the number of times a 4 actually is seen by the total number of tosses to obtain the experimental probability of rolling a 4. If the number of tosses is large and the die is pretty good, then the experimental probability should be just about equal to the theoretical probability.
You can simulate all this with a computer. You can generate a "random" number between 1 and 6 and store the result. This will represent the result of a single "roll" of the die. Say you "rolled" a 4 in this way. You can immediately increment a counter, an integer variable. In this way you keep track of the number of times a 4 is "rolled." Repeat this procedure for a large number of "rolls." When this is finished, you can divide the number of times a 4 is "rolled", that's the value of the counter, by the total number of "rolls" to get a value for the experimental probability of "rolling" a 4.
If the simulation program is correct and you roll the die enough times, the experimental probability of rolling a 4 should be close to 1/6, the theoretical probability of rolling the 4. As a general rule, the more rolls, the closer the experimental probability should be to the theoretical probability.
Craps - A program written to simulate dice tossing could be at least mildly interesting. It would be even more interesting to have the dice being tossed in the context of simulating the old gambling game, Craps. For those of you who don't know the game, here are the rules:
The game is played with two standard six-sided dice. You roll two dice. The sum of the face on this first throw is called the point.
If the point is 7 or 11, you win the game immediately.If the point is 2 or 3 or 12, you lose the game immediately.If the point is anything else (4, 5, 6, 8, 9, or 10) you continue to toss the dice until you either match the point or roll a 7. If you match the point first, you win. If you roll the 7 first, you lose. Pick up a pair of dice and try it; you'll catch on to the rules pretty quickly. In this project you are going to program the computer to simulate the play of the game.
Processing:
Import random
Processing:
You will need to count the number of times the computer wins the game, how many total times the dice were thrown and how many times the individual face values were thrown. You should initialize these 8 variables to 0.
Prompt and receive from the user a random number generator seed. Echo-print. Convert this to anint.
Activate the random number generator with random.seed(s)where sis the seed value you just entered.
Prompt and receive from the user the number of games to be played. Don't forget to echo print.
Have the computer play the number of games of Craps specified. There should be no output to the screen while this is happening.
To get a random number for a die throw use variable = random.randint(1,6). This will return an integer value in the range 1 to 6. Remember each throw of the dice requires you get two random values, one for each of the die.
When complete, write a report containing the following:
A line containing the number of games played.
A line containing the total number of times a die was tossed. (This number will be even, right?)
A line containing the numbers 1 through 6 somewhat widely separated. These will serve as headings for the numbers in the following two lines.
A line containing the number of times each of the numbers 1 through 6 was tossed. These numbers will be under the headings printed on the line above.
A line containing the experimental probability that each of the numbers 1 through 6 was tossed. These numbers will also be under the headings and printed to 4 decimal places.
A line of output containing the number of times the game was won.
A line containing the experimental probability of winning a game of Craps. This should also be printed to 4 decimal places.
There will be some places where you have to repeat code in this project.Please do that rather than using functions.
Output should be easy to read. It must be possible to tell at a glance exactly what each of the numbers means. Use white space (blank lines) to separate unrelated lines of output, but in all cases all output must fit on one page. The outline above and these adminitions will force your output to look very, very like the sample output below.
Output Specifications
A sample keyboard run and output should look something like the following.