how to use randi function with while loop?

7 ビュー (過去 30 日間)
joethomas4444
joethomas4444 2016 年 3 月 20 日
回答済み: Star Strider 2016 年 3 月 20 日
Hey, I cant seem to figure out exactly how to do this matlab problem. The problem asks to create a function that takes an input, randomly generates numbers from 1 to 1000, and then once the randomly generated number equals the input, the function gives the output of how many times it took to randomly generate the input number. The reason I am having trouble is because I know how to use a while loop that continues to generate random numbers, but I dont know how to give an output that counts how many times the while loop ran.
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 20 日
Define a variable before the loop with the value zero, and in each loop iteration increment its value by one.

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

回答 (1 件)

Star Strider
Star Strider 2016 年 3 月 20 日
You need to set up a counter. At the risk of doing part of the problem for you, there’s no way to describe it other than to code it:
count = 0;
x = 0;
while x ~= 209
count = count + 1;
x = randi(1000);
end

カテゴリ

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