Question: Write the following programs in R: 1)A function called diceSim(n) that simulates the rolling two dice and adding them together. The function returns a vector

Write the following programs in R:

1)A function called diceSim(n) that simulates the rolling two dice and adding them together. The function returns a vector that contains the number of time a 1 is rolled, a 2is rolled, a 3 Is rolled, a 4 is rolled, ... up to a '12' being rolled. Have the simulation be run ntimes, wherenis a user-supplied parameter to the function.E.g. diceSim(100) produces (0, 3, 9, 11, 15, 13, 17, 11, 10, 3, 6,2)

2)

Write a second function, called freq(samples),that takes the output of the first function and produces a vector of the observed frequency that 1 is rolled, a 2 is rolled, etc. For example when given the output from the example used in question one, x <- diceSim(100)

freq(x) you get (0, 0.03, 0.09, 0.11, 0.15, 0.13, 0.17, 0.11, 0.1, 0.03, 0.06, 0.2)

3)

Write a function called run Sim(n,sim, conv= identity) that takes a simulator function such as diceSim() and a function that converts the output of the simulator into a different format such as freq() as well as the number of times the simulator is run, and runs the simulation and produces the output after the output has been converted. For example, run Sim(n, diceSim, freq) returns the same result as question 2.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!