How to make a random number generator in matlab that is based on percentages?
matlab, random
Solution
I assume you mean randomly 25% of the time? Here's one easy way to do it:
if (rand(1) >= 0.25) %# 75% chance of falling into this case
nAsp = randi([690 720], [1 1]);
else
nAsp = randi([512 768], [1 1]);
end
If you know you're generating N of these, you can do
idx = rand(N,1);
nAsp = randi([690 720], [N 1]);
nAsp(idx < 0.25) = randi([512 768], [sum(idx < 0.25) 1]); %# replace ~25% of the numbers
Problem
I am currently using the built in random number generator. for example nAsp = randi([512, 768],[1,1]); 512 is the lower bound and 768 is the upper bound, the random number generator chooses a number from between these two values. What I want is to have two ranges for nAsp but I want one of them to get called 25% of the time and the other 75% of the time. Then gets plugged into he equation. Does anyone have any ideas how to do this or if there is a built in function in matlab already? for example nAsp = randi([512, 768],[1,1]); gets called 25% of the time nAsp = randi([690, 720],[1,1]); gets called 75% of the time