Write a Matlab code that will generate a random number, which can take only two
ID: 2319992 • Letter: W
Question
Write a Matlab code that will generate a random number, which can take only two possible values H (representing a heads outcome in a random coin toss) and T (representing a tails in a random coin toss). Generate a sequence of toss outcomes of 10 random trials. Count how many times H and T are generated out of the 10 outcomes. How can you estimate the probability of H and T in this case?
Repeat the experiment with a 1XY, 5XY and 1XYZ outcomes generated. Estimate the probabilities in each case. What do you notice?
Explanation / Answer
Consider the following values.
1XY=100
5XY=500
1XYZ=1000
Write the MATLAB code for generateRandomNumber.m
function [H, T] = generateRandomNumber(n)
H = 0;
T = 0;
for i = 1:n
rn = round(rand);
if rn == 1
H = H + 1;
else
T = T + 1;
end
end
end
fprintf("n - number of random trails ");
fprintf("H - number of heads ");
fprintf("T - number of Tailes ");
fprintf("pH - probability of H ");
fprintf("pT - probabiltiy of T ");
Write the MATLAB code and find the probabilities in 1XY=100.
n = 100
[H, T] = generateRandomNumber(n)
pH = H / n
pT = T / n
fprintf(" ")
MATLAB output:
Write the MATLAB code and find the probabilities in 5XY=500.
n = 500
[H, T] = generateRandomNumber(n)
pH = H / n
pT = T / n
fprintf(" ")
MATLAB output:
Write the MATLAB code and find the probabilities in 1XYZ=1000.
n = 1000
[H, T] = generateRandomNumber(n)
pH = H / n
pT = T / n
fprintf(" ")
MATLAB output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.