尝试定义自己的随机生成器函数时,我得到 undefined variable /参数。

代码:

function result = myrand(n, t, p, d)
    a = 200 * t + p
    big_rand = a * n
    result = big_rand / 10**d
    return;
endfunction

mrand = myrand(5379, 0, 91, 4)

错误:
>> myrand
error: 't' undefined near line 2 column 15
error: called from
myrand at line 2 column 7

最佳答案

您不能使用function关键字启动脚本。
https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html

这有效:

disp("Running...")
function result = myrand(n, t, p, d)
     a = 200 * t + p
     big_rand = a * n
     result = big_rand / 10**d
     return;
endfunction

mrand = myrand(5379, 0, 91, 4)

您应该得到:
warning: function 'myrand' defined within script file 'myrand.m'
Running ...
a =  91
big_rand =  489489
result =  48.949
mrand =  48.949

10-08 06:53