How to create a random number generator in Matlab based on percentages?

I am currently using the built-in random number generator.

eg

nAsp = randi ([512, 768], [1,1]);

512 is the lower bound and 768 is the upper bound, the random number generator picks a number between these two values.

I want to have two ranges for nAsp, but I want one of them to be called 25% of the time and the other 75% of the time. Then he connects to the equation. Anyone have any ideas how to do this or if there is a built-in function in Matlab?

eg

nAsp = randi ([512, 768], [1,1]); gets 25% of the time

nAsp = randi ([690,720], [1,1]); gets a call 75% of the time

+2


a source to share


1 answer


I'm assuming you mean random 25% of the time? Here's a simple 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 that you are generating N from them, 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

      

+6


a source







All Articles