【问题标题】:How to make a random number generator in matlab that is based on percentages?如何在matlab中制作基于百分比的随机数生成器?
【发布时间】:2010-05-19 06:27:19
【问题描述】:

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

例如

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% 的时间都被调用

【问题讨论】:

    标签: matlab random


    【解决方案1】:

    我假设你的意思是随机 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 2014-12-16
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2012-12-21
      相关资源
      最近更新 更多