clear all; close all;

S_X = [1 2 3 4]; p_X = [1/2 1/4 1/8 1/8]; n = 1e6;

SourceString = randsrc(1,n,[S_X;p_X]);
%SourceString = datasample(S_X,n,'Weights',p_X);

rf = hist(SourceString,S_X)/n; % Ref. Freq. calc.
stem(S_X,rf,'rx','LineWidth',2) % Plot Rel. Freq.
hold on
stem(S_X,p_X,'bo','LineWidth',2) % Plot pmf
xlim([min(S_X)-1,max(S_X)+1])
legend('Rel. freq. from sim.','pmf p_X(x)')
xlabel('x')
grid on