how to generate 100 random numbers between 5 and -5?

조회 수: 2 (최근 30일)
rayhan hindi
rayhan hindi 2021년 6월 3일
댓글: Steven Lord 2023년 10월 30일
if i use rand it gives me that "Index exceeds the number of array elements"

답변 (1개)

Jesús Zambrano
Jesús Zambrano 2021년 6월 3일
Hey Rayhan,
You can try with these lines of code:
a = -5;
b = 5;
N = 100;
r = a + (b-a).*rand(N,1);
Hope it helps!
  댓글 수: 3
Lil
Lil 2023년 10월 29일
hello. i tried this out but for some reason it didnt really work
Steven Lord
Steven Lord 2023년 10월 30일
Most likely, if you receive the error given in the original question, you've defined a variable named rand that prevents MATLAB from calling the rand function. To check, what does this show?
which rand
built-in (/MATLAB/toolbox/matlab/randfun/rand)
If you have a variable with that name, you'd see this instead:
rand = 42;
which rand
rand is a variable.
Clear the variable and modify your code (if necessary) to avoid creating a variable of that name.

댓글을 달려면 로그인하십시오.

카테고리

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