It is often useful to be able to simulate the throw of a fair die. Write a MATLA
ID: 653346 • Letter: I
Question
It is often useful to be able to simulate the throw of a fair die.
Write a MATLAB function, dice.m, that simulates the throw of a fair die by returning some random integer between 1 and 6 every time that it is called. (Hint: Call rand to generate a random number. Divide the possible values out of rand into six equal intervals and return the number of the interval that a given random value falls into.) Write a script, test_dice.m, that simulates 10,000 times with drawing a histogram figure and displays the first 30 values in the dice. The histogram must have title, x-label, and y-label. Show the function dice.m, the script test_dice.m, and the testing result and figure.
Explanation / Answer
function [out] = fairdie
out = randi(6,1);
end
randi(k,n) generates an n by n matrix of uniform integer random numbers from the interval 1:k, in our case a scalar (1 by 1 matrix) from the interval 1:6.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.