【问题标题】:How can we create random numbers without using any function rand in matlab?我们如何在不使用 matlab 中的任何函数 rand 的情况下创建随机数?
【发布时间】:2018-04-28 23:40:05
【问题描述】:
b=round(rand(1,20));

我不想使用函数rand,而是想知道如何创建这样的系列。

【问题讨论】:

标签: matlab


【解决方案1】:

这是一个非常有趣的问题。实际上,在我看来,最简单的方法是使用Linear-feedback Shift Register。您可以在谷歌上找到大量示例和实现(here 来自另一个 SO 问题)。

这是一个基于this code 的快速 Matlab 演示:

b = lfsr(20)

function r = lfsr(size)
    persistent state;

    if (isempty(state))
        state = uint32(1);
    end

    if (nargin < 1)
        size = 1;
    end

    r = zeros(size,1);

    for i = 1:size
        r(i) = bitand(state,uint32(1));

        if (bitand(state,uint32(1)))
            state = bitxor(bitshift(state,-1),uint32(142));
        else
            state = bitshift(state,-1);
        end
    end
end

【讨论】:

    猜你喜欢
    • 2011-11-28
    • 2019-06-23
    • 2013-02-08
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2016-12-29
    相关资源
    最近更新 更多