필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
bsd
bsd 2011년 7월 24일
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

답변 (2개)

Javier
Javier 2011년 7월 24일
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
Javier
Javier 2011년 7월 25일
I use that line to plot the values among -1 and 1,to get the same number of points in x axes and y axe.
Walter Roberson
Walter Roberson 2011년 7월 25일
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
Walter Roberson 2011년 7월 24일
If you require the mean of a finite random sample of a symmetric uniform distribution to be exactly 0, then the value of the final sample is completely determined by the sum of the other samples -- the final sample would have to be exactly the negative of the rest of the sum so that the overall sum is 0. And that implies that the last sample cannot itself be random: its value is predictable by knowing the other values. You thus cannot construct a finite random sampling with an exactly fixed mean if all values are to be random.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by