MATLAB: Create random numbers with specific fraction of a certain number.

random number generator

I want to create random 1000 integer numbers between 1 and 5 (1,2,3,4,5) but I want the percentage of these numbers to be specific. for example ( 25% of 1000 for number 2, 30% for 1, 10% for 3, 25% for 4, 10% for 5).
Thank you

Best Answer

p = [.3; .25; .1;.25; .1]*1000;
z = (1:5)';
out = repelem(z,p);
out = out(randperm(numel(out)));
or with Statistics and Machine Learning Toolbox
out = randsample (1:5, 1000, true, [.3; .25; .1;.25; .1])';