How to Generate a Random # Between -1 and 1?

5 次查看(过去 30 天)
Hello,
I have a simple question. I want to produce a random number between -1 and 1 as the title states. I tried doing this already:
randnum = rand(1)
But this will always be positive because it will generate between 0 and 1. So I tried this:
randnum = rand([-1,1])
But I receive an empty, double column vector.

采纳的回答

Image Analyst
Image Analyst 2018-11-26
Did you look in the help where is told you how to get a number in the range from a to b?
r = 2 * rand - 1;
  1 个评论
Tom Keaton
Tom Keaton 2018-11-27
I did, but I from the information there, I couldn't figure out how to apply it to this specific case. Thank you for the answer!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by