       Re: how to get random numbers from a distribution

• To: mathgroup at smc.vnet.net
• Subject: [mg81512] Re: how to get random numbers from a distribution
• From: Roman <rschmied at gmail.com>
• Date: Wed, 26 Sep 2007 06:43:31 -0400 (EDT)
• References: <fd7s2q\$cfd\$1@smc.vnet.net>

```For a simple distribution like x^2 (assuming on an interval [0,1]) you
can go by integration. First, define the distribution and the
definition interval:

d[x_] = x^2;
a = {0,1};

Then, find the normalized cumulative integral of the distribution
function:

i[y_] = Integrate[d[x],{x,a[],y}]/Integrate[d[x],
{x,a[],a[]}];

We know that i[a[] = 0 and i[a[]]=1 by definition. You just need
to invert this function now:

FullSimplify[Solve[i[y]==p,y],0<p<1]

This gives three solutions, only one of which is real-valued: p^(1/3).
You define a new function by picking the correct solution:

g[p_] = p^(1/3);

>From this you can now produce random numbers distributed as d[x] by
transforming the uniform numbers produced by RandomReal on the
interval [0,1]. For example, to produce ten random numbers in the
interval defined by "a" and distributed like d[x], you do

g/@RandomReal[{0,1},10]

Obviously this is limited to analytically integrable *and* invertible
distributions d[x]. Also, the resulting algorithm may be far from
optimal (speed-wise), as the example d[x]=e^(-x^2) with a={-
Infinity,Infinity} shows: it gives g[x]=InverseErf[2p-1], which is a
slow special function, whereas there are well-known alternative
algorithms for producing fast Gaussian random numbers.

Roman.

On Sep 24, 10:21 am, tdoxm... at gmail.com wrote:
> Hi ya alll
>
> I am a new Mathematica user.
> I have a function that i use to generate a probability distribution
> (eg x^2). I want a random number generator, that should generate
> values from this probability distribution only,
>
> I know this Random function in mathematica, that can generate in
> particular range, but how do i tell to generate from a particular
> distribution.
>
> I created a list of values using my probability function but not able
> to integrate it with Random function so that random numnber takes
> value from that  list.
>
> Also I googled and saw some function like RandomChoice and some other
> but they are available in Mathematica6 and i use 5.2.
>
> Can u help with this.
>