Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to generate random number given density function?

Subject: How to generate random number given density function?

From: Tiên

Date: 27 Nov, 2010 11:37:04

Message: 1 of 4

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

Subject: How to generate random number given density function?

From: ImageAnalyst

Date: 27 Nov, 2010 14:39:33

Message: 2 of 4

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.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289740#772962

Subject: How to generate random number given density function?

From: Roger Stafford

Date: 27 Nov, 2010 19:16:03

Message: 3 of 4

"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

Subject: How to generate random number given density function?

From: Roger Stafford

Date: 28 Nov, 2010 03:43:03

Message: 4 of 4

"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

or on ImageAnalyst's referenced thread.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us