Generate positive random numbers

14 ビュー (過去 30 日間)
Charlie Harris
Charlie Harris 2016 年 8 月 14 日
コメント済み: Charlie Harris 2016 年 8 月 14 日
I need to write a function for the area of a circle. If area = pi * r.^2. How can I ensure that a randomly generated value for r will be positive?... r = rand was rejected by my teacher. I assume because r could be negative?

採用された回答

Image Analyst
Image Analyst 2016 年 8 月 14 日
r = rand;
will give a positive number between 0 and 1. It will never be negative. You'll have to ask your teacher why he or she rejected that. Perhaps he/she wanted it multiplied by something, like r needs to go between 0 and 15:
r = 15 * rand(1);
Maybe you didn't write a function but just wrote a script. Maybe the function needed to have input and output arguments and you didn't give them. It's hard to say without seeing (1) your code that you showed your teacher, (2) the problem statement, and (3) your exact teacher comments. You'll have to ask your teacher for clarification.
  1 件のコメント
Charlie Harris
Charlie Harris 2016 年 8 月 14 日
Thank You! I didn't realize rand had a range between 0 and 1. p.s.I am going to repost with much more detail

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by