MathGroup Archive 2007

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

Search the Archive

Compiling Random Numbers from a distribution

  • To: mathgroup at smc.vnet.net
  • Subject: [mg84483] Compiling Random Numbers from a distribution
  • From: Asim <maa48 at columbia.edu>
  • Date: Fri, 28 Dec 2007 04:11:29 -0500 (EST)

Hi

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}},
  Module[
   {m},
   x = RandomReal[NormalDistribution[0.0, 1.0]];
   m = 2 + x;
   Clear[x];
   m
   ]
  ]



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}},
  Module[
   {m, x},
   x = RandomReal[NormalDistribution[0.0, 1.0]];
   m = 2 + x;
   m
   ]
  ]

as can be seen from the output from Mathematica 6.01 on windows

In[20]:= v1[0.0, 1.0]

During evaluation of In[20]:= CompiledFunction::cfse: Compiled \
expression NormalDistribution[0.,1.] should be a machine-size real \
number. >>

During evaluation of In[20]:= CompiledFunction::cfex: Could not \
complete external evaluation at instruction 3; proceeding with \
uncompiled evaluation. >>

Out[20]= 2.78038


I would appreciate it very much if someone can explain why this
happens. I would much prefer if the second function (v1) were able to
compile. I need to use random numbers from the normal distribution to
work in much more complicated functions, and I am not sure whether the
approach in the first function (v) is really fine.

Thanks

Asim Ansari


  • Prev by Date: Re: Loading Packages in a loop
  • Next by Date: Re: problem with exporting a graph
  • Previous by thread: Re: problem with exporting a graph
  • Next by thread: Re: Compiling Random Numbers from a distribution