Path: news.mathworks.com!not-for-mail
From: "Omega69 Omega69" <Omega69@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Help with Random Variable Generation
Date: Fri, 16 Jan 2009 17:44:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <gkqh11$osq$1@fred.mathworks.com>
Reply-To: "Omega69 Omega69" <Omega69@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1232127841 25498 172.30.248.37 (16 Jan 2009 17:44:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 16 Jan 2009 17:44:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1673625
Xref: news.mathworks.com comp.soft-sys.matlab:512094


Hi everybody,
I'm trying to generate a random variable which probability density function is:
f(x)=1-|x|,    -1<=x<=1
To do that, I have calculated cumulative distribution function, which is (if I have not made any mistake...):
F(x)= 0          if         x<-1
          x?/2+x+1/2       if      -1<=x<=0
         -x?/2+x+1/2       if       0<=x<=-1
          1                       if        x>1

Now, I declare u=F(x) (where u is a uniform variable), and I try to isolate x to get F^-1(u), and then declare them in Matlab, but here is where I have problems:

1) I know how to isolate x when -1<=x<=0, and I get x=sqrt(2*u)-1, but I don't know how to isolate it when 0<=x<=-1, any idea for this?

2) And the other part is that I don't know how to write this into Matlab. I have try this:
N=1000;
u1=rand(N,1);
u2=rand(N,2)
X=(sqrt(2*u1)-1)*(u2(N,1)<0,5)+((the thing I don't know how to isolate))*(u2>0.5);

But the results I think that are wrong.

Anyone has any idea to get this correct?

Thank you very much in advance!!