I want my code to run continuously.

1 回表示 (過去 30 日間)
Kelly
Kelly 2013 年 12 月 5 日
回答済み: sixwwwwww 2013 年 12 月 5 日
In a .m file I wrote the following. g = randi(3,1)
When I press f5 and run it. It will give me a random number from 1 to 3. However, it will not change.
E.G
>>> g
1
>>> g
1
>>> g
1
g will forever be 1, what can I do that so that when I run my code, each time i type "g" it will give me a different random number from 1 to 3?

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 12 月 5 日
You are not defining a function, you are defining a variable. You can use this, provided you save it in the file "g.m"
function r = g
r = randi(3,1);
end
You cannot use this at the MATLAB command prompt, you must write it to a file.

sixwwwwww
sixwwwwww 2013 年 12 月 5 日
try to run code again and again it will give you different values from 1 to 3 each time

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by