MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compiling Random Numbers from a distribution

  • To: mathgroup at
  • Subject: [mg84488] Re: Compiling Random Numbers from a distribution
  • From: Bill Rowe <readnewsciv at>
  • Date: Sat, 29 Dec 2007 02:57:28 -0500 (EST)

On 12/28/07 at 4:11 AM, maa48 at (Asim) wrote:

>I am wondering why the following function works in Mathematica 6.01,
>(notice, x is not a local variable in the function below)

>v = Compile[{{mu, _Real}, {s, _Real}},
>x = RandomReal[NormalDistribution[0.0, 1.0]];
>m = 2 + x;

>In[19]:= v[0.0, 1.0]

>Out[19]= 2.59192

>but the compilation in the following function which makes the
>variable x local to the function does not work

>v1 = Compile[{{mu, _Real}, {s, _Real}},
>{m, x},
>x = RandomReal[NormalDistribution[0.0, 1.0]];
>m = 2 + x;

I don't have an answer to your specific question but I question
the need for using Compile. I assume you are using this to
improve execution speed. If so, it appears to me (at least on my
machine) Compile doesn't really help here. Using your first
definition above for v:

In[2]:= Timing[Do[v[0., 1.];, {100000}]]

Out[2]= {3.82615,Null}


In[3]:= Timing[Do[RandomReal[NormalDistribution[2., 1.]];, {100000}]]

Out[3]= {0.858795,Null}

and faster is

In[4]:= Timing[
  data = RandomReal[NormalDistribution[2., 1.], 100000];]

Out[4]= {0.035228,Null}

Here, I've taken advantage of the fact if x has a normal
distribution with mean m and standard deviation s then x+const
has a normal distribution with mean m+const and standard
deviation s.

In[6]:= $Version

Out[6]= 6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007)
To reply via email subtract one hundred and four

  • Prev by Date: Re: Creating an application
  • Next by Date: Re: help with mathematica 5.2-Vector
  • Previous by thread: Re: Compiling Random Numbers from a distribution
  • Next by thread: Solving stiff differential equations