How to create a set of random numbers
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, how I could generate random number 90 times
- probability space are 1, 2, 3
- totally 90 random numbers
- got 21 of 1, 38 of 2 and 31 of 3
Could you give me some solutions?
Thanks Joy
0 commentaires
Réponse acceptée
Stephen23
le 30 Mar 2017
Modifié(e) : Stephen23
le 30 Mar 2017
You could use randperm to randomly arrange a vector of exactly those numbers:
>> vec = [repmat(1,21,1);repmat(2,38,1);repmat(3,31,1)]; % or use REPELEM
>> vec = vec(randperm(numel(vec)));
And checking that it fulfills your requirements:
>> numel(vec)
ans = 90
>> nnz(vec==1)
ans = 21
>> nnz(vec==2)
ans = 38
>> nnz(vec==3)
ans = 31
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Random Number Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!