Generating random integer between negative and positive range

16 ビュー (過去 30 日間)
N/A
N/A 2016 年 10 月 27 日
回答済み: Walter Roberson 2016 年 10 月 27 日
I'm trying to generate a random integer in the range of -5 and +5 using round and rand functions. I'm able to generate a random integer however it always returns a negative value and a zero and not in the expected range.
Obviously the easiest solution to this would be:
round(unifrnd(-5,5));
But I have to use rand function specifically along with round to produce this random number.
I would very much appreciate any advice and help.

回答 (2 件)

KSSV
KSSV 2016 年 10 月 27 日
randi([-5 5],1)
doc randi
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 27 日
That does not use rand() and round() specifically.

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


Walter Roberson
Walter Roberson 2016 年 10 月 27 日

カテゴリ

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