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

2 views (last 30 days)
rayhan hindi on 3 Jun 2021
Commented: Steven Lord on 30 Oct 2023
if i use rand it gives me that "Index exceeds the number of array elements"
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Answers (1)

Jesús Zambrano on 3 Jun 2021
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 CommentsShow 1 older commentHide 1 older comment
Lil on 29 Oct 2023
hello. i tried this out but for some reason it didnt really work
Steven Lord on 30 Oct 2023
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.

Sign in to comment.

### Categories

Find more on Random Number Generation in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!