# how to generate a uniform random variable in the interval [-1 1] with mean zero?

9 views (last 30 days)

Show older comments

Dear sir/madam,

I am working random variables. I need to know how can I generate a uniform random variable in the interval [-1 1], and also with mean zero? The function rand() generates a uniform random variable in the interval [0 1]. Looking forward to hearing from you soon.

Thanking you, BSD

##### 0 Comments

### Answers (2)

Javier
on 24 Jul 2011

You could use this code:

a=-1;b=1;

r = a + (b-a).*rand(100,1);

v=[-1:0.02:1];

v(:,v==0)=[];

plot(v,r)

Use help rand for more information!

##### 4 Comments

Javier
on 25 Jul 2011

Walter Roberson
on 25 Jul 2011

If you were going to do that, then I would suggest using

v=[-1:0.02:1];

first, and then using

r = a + (b-a).*rand(length(v),1);

However, I don't think plot(v,r) gives anything useful: there is no reason I can see to use [-1,1] as your X axes. You are, after all, not creating a PDF or CDF.

Walter Roberson
on 24 Jul 2011

##### 1 Comment

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!