http://nl.mathworks.com/matlabcentral/newsreader/view_thread/297479
MATLAB Central Newsreader  How to generate random number given density function?
Feed for thread: How to generate random number given density function?
enus
©19942017 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://nl.mathworks.com/images/membrane_icon.gif

Sat, 27 Nov 2010 11:37:04 +0000
How to generate random number given density function?
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/297479#799738
Tiên
Hi All, <br>
I need generate a random number x belongs to [0, 2*pi], given density function <br>
f(x) = 1/ (2*pi) * { (a^2  1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1. <br>
Could you help me to solve the problem? Thank you very much. <br>
Nguyen Van Tien

Sat, 27 Nov 2010 14:39:33 +0000
Re: How to generate random number given density function?
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/297479#799752
ImageAnalyst
In short, you need to compute the cumulative distribution function and<br>
then invert it and stick in the uniform distribution. See #6 in this<br>
thread where I do the process for a Ralyeigh distribution.<br>
<a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/289740#772962">http://www.mathworks.com/matlabcentral/newsreader/view_thread/289740#772962</a>

Sat, 27 Nov 2010 19:16:03 +0000
Re: How to generate random number given density function?
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/297479#799806
Roger Stafford
"Tiên " <nguyenvantien812@gmail.com> wrote in message <icqqh0$19p$1@fred.mathworks.com>...<br>
> Hi All, <br>
> I need generate a random number x belongs to [0, 2*pi], given density function <br>
> f(x) = 1/ (2*pi) * { (a^2  1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1. <br>
> Could you help me to solve the problem? Thank you very much. <br>
> Nguyen Van Tien<br>
        <br>
You'll be glad to learn that the cumulative distribution function for your f(x) not only has a fairly simple expression, its inverse is also readily computable. You can look up the integral for the cumulative distribution in any good table of integrals. If you have trouble, I'll give integral formula to you.<br>
<br>
Roger Stafford

Sun, 28 Nov 2010 03:43:03 +0000
Re: How to generate random number given density function?
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/297479#799885
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <icrldj$qo9$1@fred.mathworks.com>...<br>
> "Tiên " <nguyenvantien812@gmail.com> wrote in message <icqqh0$19p$1@fred.mathworks.com>...<br>
> > Hi All, <br>
> > I need generate a random number x belongs to [0, 2*pi], given density function <br>
> > f(x) = 1/ (2*pi) * { (a^2  1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1. <br>
> > Could you help me to solve the problem? Thank you very much. <br>
> > Nguyen Van Tien<br>
>         <br>
> You'll be glad to learn that the cumulative distribution function for your f(x) not only has a fairly simple expression, its inverse is also readily computable. You can look up the integral for the cumulative distribution in any good table of integrals. If you have trouble, I'll give integral formula to you.<br>
> <br>
> Roger Stafford<br>
       <br>
You haven't asked but I've decided to give you the cumulative distribution of that function you defined in case the integral table formulas proved to be too confusing. It is:<br>
<br>
F(x) = 1/pi*atan((a+1)/(a1)*tan((xpi)/2))+1/2<br>
<br>
for the range of numbers 0 <= x <= 2*pi. It is the definite integral of your f(x) from 0 to x. It assumes that a > 1. You will note that when x = 0, it is zero and when x = 2*pi, it is one, as a valid cumulative distribution should be. Its derivative is just f(x), though that takes some manipulation to demonstrate.<br>
<br>
As you can see, the inverse of this function can readily be found and this will enable you to generate random variables with your given density function using the 'rand' function in accordance with a standard method which you can read about at:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">http://en.wikipedia.org/wiki/Inverse_transform_sampling</a><br>
<br>
or on ImageAnalyst's referenced thread.<br>
<br>
Roger Stafford