This problem studies how to generate random numbers accordin
This problem studies how to generate random numbers according to a specific PMF. Consider a discrete PMF p_X (k) = [0.3 0.1 0.1 0.2 0.1 0.05 0.05 0.05 0.05]. Write a MATLAB function that takes this PMF and generates N = 100,000 realizations of X. Your function can only use the uniform random number generator rand and no other random number generators. Submit your code and the empirical histogram of X.
Solution
%// First I created some random data N=1e6; x=randn(1,N);x=(x-min(x))/(max(x)-min(x)); pmf=hist(x,101)./N; %// It\'s a normal distribution but scaled to output to [0 1] %// First, get the cdf xh=cumsum(pmf); %// Create the list of possible values of this random variable Y=20:.1:30; %// Create a uniformly distributed random variable between 0 and 1 R=rand() %// Find where the cdf value is closest to R [~,J]=min(abs(xh-R)); %// Find the resulting sample values Y(J) %// Just a little plot to make sure it\'s working hold on plot(Y,xh) plot([20 30],[R R],[Y(J) Y(J)],[0 1]) hold off![This problem studies how to generate random numbers according to a specific PMF. Consider a discrete PMF p_X (k) = [0.3 0.1 0.1 0.2 0.1 0.05 0.05 0.05 0.05]. W This problem studies how to generate random numbers according to a specific PMF. Consider a discrete PMF p_X (k) = [0.3 0.1 0.1 0.2 0.1 0.05 0.05 0.05 0.05]. W](/WebImages/39/this-problem-studies-how-to-generate-random-numbers-accordin-1119363-1761595304-0.webp)