Loop / Repeat if random number = 0

2 ビュー (過去 30 日間)
Mark
Mark 2012 年 6 月 12 日
I have good and fun code and it is working very well.
However, I am getting an error when I get zero (0) from my "random integer generator":
n=round(200.*rand)
Is there any way I can tell the script to rerun the calculation for n, if n=zero? I am unable to change the variable n, and despite the low chance of getting two n=0 I want to "reroll" until n is definitely not equal to zero. Any idea how to do this?

採用された回答

Kye Taylor
Kye Taylor 2012 年 6 月 12 日
Try
n = round(200*rand);
while (n == 0)
n = round(200*rand);
end
Or, even better,
n = randi(200); % generates random integer between 1 and 200
  1 件のコメント
Greg Heath
Greg Heath 2012 年 6 月 12 日
n = 1+round(199*rand)
Greg

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by