Write a Matlab code that will generate a random number, which can take only two
ID: 3935618 • 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 arc 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 IXYZ outcomes generated. Estimate the probabilities in each case. What do you notice? XYZ represent the last 3 digits of your student ID number Example: Assume Student ID 20101234 which corresponds to X-2. Y=3 and Z - 4.Explanation / Answer
Important: Change the values of n according to your student ID number.
Code
%For 10 random trials
%generating 10 random numbers
prob10 = randi([0 1], 1, 10);
%initialising number of heads
H = 0;
%iterating through the number of trials
for i=1:1:10
if prob10(i)==0
H = H + 1;
end
end
%displaying probabilities
disp(['Probability of heads for 10 trials = ', num2str(H/10.0)]);
disp(['Probability of tails for 10 trials = ', num2str(1-(H/(10.0)))]);
%For 1XY random trials
n = 123;
%generating n random numbers
prob1XY = randi([0 1], 1, n);
%initialising number of heads
H = 0;
%iterating through the number of trials
for i=1:1:n
if prob1XY(i)==0
H = H+1;
end
end
%displaying probabilities
disp(['Probability of heads for 123 trials = ', num2str(H/(n*1.0))]);
disp(['Probability of tails for 123 trials = ', num2str(1-(H/(n*1.0)))]);
%For 5XY random trials
n = 523;
%generating n random numbers
prob5XY = randi([0 1], 1, n);
%initialising number of heads
H = 0;
%iterating through the number of trials
for i=1:1:n
if prob5XY(i)==0
H = H+1;
end
end
%displaying probabilities
disp(['Probability of heads for 523 trials = ', num2str(H/(n*1.0))]);
disp(['Probability of tails for 523 trials = ', num2str(1-(H/(n*1.0)))]);
%For 1XYZ random trials
n = 1234;
%generating 10 random numbers
prob1XYZ = randi([0 1], 1, n);
%initialising number of heads
H = 0;
%iterating through the number of trials
for i=1:1:n
if prob1XYZ(i)==0
H = H+1;
end
end
%displaying probabilities
disp(['Probability of heads for 1234 trials = ', num2str(H/(n*1.0))]);
disp(['Probability of tails for 1234 trials = ', num2str(1-(H/(n*1.0)))]);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.