Write a MATLAB code that will generate a random variable whi
Write a MATLAB code that will generate a random variable, which can take only five possible integer values 10,11,12,13, and 14. Generate a sequence of outcomes of 100 random trials. Count how many times 10 and 14 are generated out of the 100 outcomes. How can you estimate the probability of 10 and 14 in this case? Repeat the experiment with a 1 000, 5000 and 10000 outcomes generated. Estimate the probabilities in each case. What do you notice?
Thank you for helping :)
Solution
The MATLAB code for the problem is,
clear all
clc
prompt = \'Enter the number of samples:\';
N = input(prompt);
count10=0; % initiate the count for number 10
count14=0; % initiate the count for number 14
for i=1:N
outcome= randi([10 14],N,1);
end
for i=1:N % to count 10 & 14
if outcome(i)==10 % if outcome is 10 count increase
count10=count10+1;
elseif outcome(i)==14 % if outcome is 14 count increase
count14=count14+1;
end
end
prob_10 = count10/N; % probablity of occurance of 10
prob_14 = count14/N; % probablity of occurance of 14
fprintf(\'10 is generated %d times & 14 is generated %d times \ \', count10, count14);
fprintf(\'Probablity of occurance of 10 is %d \ Probablity of occurance of 14 is %d \ \', prob_10, prob_14);
The results of the MATLAB code are,
Enter the number of samples:100
10 is generated 26 times & 14 is generated 24 times
Probablity of occurance of 10 is 2.600000e-01
Probablity of occurance of 14 is 2.400000e-01
Enter the number of samples:1000
10 is generated 194 times & 14 is generated 190 times
Probablity of occurance of 10 is 1.940000e-01
Probablity of occurance of 14 is 1.900000e-01
Enter the number of samples:5000
10 is generated 1000 times & 14 is generated 998 times
Probablity of occurance of 10 is 2.000000e-01
Probablity of occurance of 14 is 1.996000e-01
Enter the number of samples:10000
10 is generated 2060 times & 14 is generated 1927 times
Probablity of occurance of 10 is 2.060000e-01
Probablity of occurance of 14 is 1.927000e-01
By increasing number of outcomes generated, the probability value gets more precise. The time taken for the computation also increased.

