如何在matlab中创建一个基于百分比的随机数生成器?

Ben*_*sen 2 random matlab

我目前正在使用内置随机数生成器.

例如

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

512是下限,768是上限,随机数发生器从这两个值之间选择一个数字.

我想要的是nAsp有两个范围,但我希望其中一个在25%的时间被调用,另一个在75%的时间被调用.然后插入他的等式.有没有人有任何想法如何做到这一点,或者在matlab中是否有内置函数?

例如

nAsp = randi([512,768],[1,1]); 被叫25%的时间

nAsp = randi([690,720],[1,1]); 75%的时间被调用

mtr*_*trw 6

我假设你的意思是随机的25%的时间?这是一个简单的方法:

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
Run Code Online (Sandbox Code Playgroud)

如果你知道你正在生成这些中的N个,你就可以做到

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
Run Code Online (Sandbox Code Playgroud)