COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to generate random number given density function?

• Follow

```Hi All,
I need generate a random number x belongs to [0, 2*pi], given density function
f(x) = 1/ (2*pi) * { (a^2 - 1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1.
Could you help me to solve the problem? Thank you very much.
Nguyen Van Tien
```
 0

```In short, you need to compute the cumulative distribution function and
then invert it and stick in the uniform distribution.  See #6 in this
thread where I do the process for a Ralyeigh distribution.
```
 0

```"Tiên " <nguyenvantien812@gmail.com> wrote in message <icqqh0\$19p\$1@fred.mathworks.com>...
> Hi All,
> I need generate a random number x belongs to [0, 2*pi], given density function
>    f(x) = 1/ (2*pi) * { (a^2 - 1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1.
> Could you help me to solve the problem? Thank you very much.
> Nguyen Van Tien
- - - - - - - - -
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.

Roger Stafford
```
 0

```"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <icrldj\$qo9\$1@fred.mathworks.com>...
> "Tiên " <nguyenvantien812@gmail.com> wrote in message <icqqh0\$19p\$1@fred.mathworks.com>...
> > Hi All,
> > I need generate a random number x belongs to [0, 2*pi], given density function
> >    f(x) = 1/ (2*pi) * { (a^2 - 1) / ( a^2 + 2*a*cos(x) + 1) } where a >=1.
> > Could you help me to solve the problem? Thank you very much.
> > Nguyen Van Tien
> - - - - - - - - -
>   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.
>
> Roger Stafford
- - - - - - - -
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:

F(x) = 1/pi*atan((a+1)/(a-1)*tan((x-pi)/2))+1/2

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.

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:

http://en.wikipedia.org/wiki/Inverse_transform_sampling

Roger Stafford
```
 0

3 Replies
497 Views

Similiar Articles:

7/24/2012 5:12:16 AM