How to generate random number within a sum limit?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Generate 5 random numbers, where their sum should be less/equal to 50.
0 Commenti
Risposta accettata
Più risposte (1)
Roger Stafford
il 7 Nov 2017
If the numbers are supposed to be any five positive integers, do this:
b = true;
while b
x = 50*randi(5,1);
b = sum(x)>50;
end
If the numbers can be any non-negative real numbers, change the third line above to:
x = 50*rand(5,1);
0 Commenti
Vedere anche
Categorie
Scopri di più su Random Number Generation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!