필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Tom Keaton
Tom Keaton 2018년 11월 26일
댓글: Tom Keaton 2018년 11월 27일
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