Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 4 Write a program that simulates the rolling of two die. Ask the user f

ID: 2292369 • Letter: E

Question

Exercise 4 Write a program that simulates the rolling of two die. Ask the user for a seed value to initialize the srand library function. The program > » should use the rand function (random number generator) to roll the first die, and then use rand again to roll the second die. The sum of the two values should then be calculated. Make your program rolls the dice 50,000 times. Use a 1-D array to store the number of times each sum appeared. Print the results in a tabular format, along with the percentage that each sum appeared, ie., (count/50,000)* 100% Note: Since each die has an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12. The total possible number of combinations is 36. The value 7 will be the most frequent sum with six possible combinations ((16),(6,1),(25),(5,2), {3,4),(4,3) 6/36-16.7% of the time). The values 2 and 12 are the least frequent sums ({1,1}-> 1/36-2.8%, and {6,6}-1/36- 2.8%). In your simulation, the calculated percentages should be similar to the theoretical values Sample Run: Enter seed value 862 SumCount Appear 2 1365 2.73% 3: 2796 5.59% 4: 4283 8.57% 5w: 5546 11.09% 6': 6843 13.69% 7 8278 16.56% 6993 13.99% 2 5504 11.01% 10: 4187 8.37% 11: 2770 5.54% 12: 1435 2.87%

Explanation / Answer

ANSWER.

A Program that simulates the rollingof two die. Askthe user for a seed value to initialize the srand libary function.The program should use the rand function to roll the first die,and then use rand agian to roll the second die.


Matlab Code:


%% rolling of two die


imax = 6;

X = zeros(1,50000);

for cc = 1:length(X)

X(cc) = randi(imax) + randi(imax);
end

freq_x = zeros(1,11);

for cc = 1:11

freq_x(cc) = sum(X==(cc+1));
end

clc;

fprintf('=====SUM======COUNT======Percent ')

for cc = 1:11

fprintf('=====%d======%d======%f ',cc+1,freq_x(cc),freq_x(cc)*100/50000);
end

Output:

=====SUM======COUNT======Percent
=====2======1366======2.732000
=====3======2777======5.554000
=====4======4202======8.404000
=====5======5415======10.830000
=====6======6924======13.848000
=====7======8446======16.892000
=====8======6999======13.998000
=====9======5550======11.100000
=====10======4176======8.352000
=====11======2805======5.610000
=====12======1340======2.680000