Write a MATLAB function that would generate n random numbers
Write a MATLAB function that would generate n random numbers using the ‘rand’ and ‘randn’ functions in any given range. The function should output the mean, standard deviation and confidence interval of the ‘n’ numbers generated. Show your function output for three cases: 1) when n=1, 2) n > 50, and 3) n < 30.
A 90 percent confidence can be assumed.
Solution
% this is the function we are taking only one input which is n
function [X,m,sd,ci90] = func(n)
% generating random number
X = rand(n,1);
% calculating mean
m = mean(X);
% calculating standard deviation
sd = std(X);
% this block will calculate the confidence interval
ci = 0.9; % as we need 90 percent confidence
alpha = 1 - ci;
T_multiplier = tinv(1-alpha/2, n-1);
ci90 = T_multiplier*sd/sqrt(n);
end
Output example :
