登录论坛

查看完整版本 : 如何在Matlab中创建一个基于百分比的随机数生成器?


poster
2019-12-10, 20:41
我目前正在使用内置的随机数生成器。

例如

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%的时间



回答:

我假设您的意思是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 如果您知道要生成其中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

更多&回答... (https://stackoverflow.com/questions/2863320)