How to generate a random decimal number between -1 and 1?

5 ビュー (過去 30 日間)
Wen Shu Wong
Wen Shu Wong 2020 年 10 月 29 日
編集済み: Stephen23 2020 年 10 月 29 日
I'm trying to use rand below but it is giving me [ ].
rand([-1 1]);

採用された回答

Stephen23
Stephen23 2020 年 10 月 29 日
編集済み: Stephen23 2020 年 10 月 29 日
The inputs to rand define its output size, not the output value range. Its range is fixed to 0..1, but you can adjust generate other values using some simple operations:
2*rand()-1
ans = -0.7192

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 10 月 29 日
rand*2-1
Note this will not generate exactly -1 or exactly 1

カテゴリ

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