Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

RStudio Write a function named keepRollingUntilSnakeEyes. The function should not take ANY parameters. The function should keep calling rollDice(2) inside of a loop to simulate

RStudio

Write a function named keepRollingUntilSnakeEyes. The function should not take ANY parameters. The function should keep calling rollDice(2) inside of a loop to simulate multiple rolls of two dice. The loop should stop when the roll is two ones (i.e. "snake eyes"). The function should display the values each roll as shown below. The function should return the total number of rolls that were made. See the examples below. Note that in the first two examples below, the last value displayed is the value that is "returned". In the third example below, the return value is captured in a variable and is displayed in a separate command. (also see the next question). HINTS: a. Use the cat function to display the messages. b. Use a variable to keep track of how many rolls took place c. keep looping until you get a 1 and a 1 EXAMPLES: > keepRollingUntilSnakeEyes() # the return value appears after all the messages roll #1 was: 2 and 1 roll #2 was: 3 and 5 roll #3 was: 6 and 5 roll #4 was: 4 and 1 roll #5 was: 5 and 5 roll #6 was: 6 and 1 roll #7 was: 2 and 3 roll #8 was: 6 and 3 roll #9 was: 5 and 4 roll #10 was: 6 and 3 roll #11 was: 6 and 3 roll #12 was: 4 and 5 roll #13 was: 4 and 1 roll #14 was: 4 and 2 roll #15 was: 1 and 1 [1] 15 > keepRollingUntilSnakeEyes() # the return value appears after all the messages roll #1 was: 1 and 1 [1] 1 > numRolls <- keepRollingUntilSnakeEyes() # return value is captured in a variable roll #1 was: 2 and 1 roll #2 was: 4 and 3 roll #3 was: 3 and 6 roll #4 was: 3 and 6 roll #5 was: 4 and 3 roll #6 was: 6 and 4 roll #7 was: 6 and 2 roll #8 was: 3 and 2 roll #9 was: 4 and 1 roll #10 was: 1 and 1 > numRolls # we can now display the value that was returned from the function [1] 10 6. Modify the function that you created in the previous question, keepRollingUntilSnakeEyes. In this new version you should define a single argument named, showOutput. The default value of showOutput should be FALSE. If showOutput is TRUE then the messages should be displayed. If showOutput is FALSE then the messages should NOT be displayed. In either case, as with the last question, the functions should return total number of rolls. For example: > keepRollingUntilSnakeEyes() # this will not show output [1] 48 > keepRollingUntilSnakeEyes(showOutput = FALSE) # nor will this [1] 80 > keepRollingUntilSnakeEyes(FALSE) # nor will this [1] 1 > keepRollingUntilSnakeEyes(TRUE) # this WILL show output roll #1 was: 4 and 4 roll #2 was: 4 and 6 roll #3 was: 5 and 4 roll #4 was: 4 and 3 roll #5 was: 5 and 6 roll #6 was: 5 and 5 roll #7 was: 2 and 3 roll #8 was: 3 and 1 roll #9 was: 1 and 3 roll #10 was: 3 and 4 roll #11 was: 3 and 2 roll #12 was: 1 and 2 roll #13 was: 6 and 3 roll #14 was: 1 and 1 [1] 14 > keepRollingUntilSnakeEyes(showOutput = TRUE) # this WILL show output roll #1 was: 2 and 4 roll #2 was: 6 and 1 roll #3 was: 4 and 2 roll #4 was: 5 and 4 roll #5 was: 5 and 6 roll #6 was: 1 and 1 [1] 6 7. Do all of the following steps: a. Write a function named playManyTimes that calls the function keepRollingUntilSnakeEyes in a loop. playManyTimes should take an argument, n, that indicates the number of times the game should be played. playManyTimes should return a vector that contains the number of rolls it took each time the keepRollingUntilSnakesEyes function was called. For example: > playManyTimes(3) [1] 66 1 22 > playManyTimes(10) [1] 6 27 35 106 38 51 100 1 1 26 b. Run results <- playManyTimes(10000) to capture the results of playing the game ten thousand times. c. Create a histogram of the results with the command, h <- hist(results) The histogram should look similar to the example shown below. You can see from this histogram that the function keepRollingUntilSnakeEyes is much more likely to return smaller numbers than to return larger numbers: d. In the previous step the command h <- hist(results) captured the output of the hist function in the variable h You can display the contents of this variable to examine details about the histogram (see the output below) .The information in h is stored in a structure called a "list"., You can access the information in h. For example, the counts entry in h contains the number of values in the results variable that fell into each "bar" of the histogram. The sum of all these counts are 10,000,as should be expected. For example: > h $breaks [1] 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 $counts [1] 4369 2427 1434 758 432 258 136 78 55 25 12 5 6 1 [15] 3 0 1 $density [1] 0.021845 0.012135 0.007170 0.003790 0.002160 0.001290 0.000680 0.000390 [9] 0.000275 0.000125 0.000060 0.000025 0.000030 0.000005 0.000015 0.000000 [17] 0.000005 $mids [1] 10 30 50 70 90 110 130 150 170 190 210 230 250 270 290 310 330 $xname [1] "results" $equidist [1] TRUE attr(,"class") [1] "histogram" > sum(h$counts) [1] 10000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions