MATLAB Answers

Create random numbers with specific fraction of a certain number.

6 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Jul 2017
Edited: Andrei Bobrov on 7 Jul 2017
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])';

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!