generate a random number
Ältere Kommentare anzeigen
Hi,I need to generate a random number distribution that has different weights. For example: I want to generate 1000000 numbers with 80% of the generated values being less than 5. How do I do this?
1 Kommentar
Antworten (1)
Image Analyst
am 16 Dez. 2016
Is this what you want? Try this:
n = 1000000; % Number of random numbers you want to generate.
maxValue = 5 % Number that you want to specify some percentage of numbers are less than.
percentage = 80; % Range is 0 to 100
newMaxValue = maxValue * 100 / percentage
% Compute random numbers.
r = newMaxValue * rand(1, n);
% Compute percentage under maxValue
pct = sum(r < maxValue) / n * 100
% Plot it.
histogram(r, 'Normalization', 'pdf');
grid on;

Kategorien
Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!