MathGroup Archive 2005

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

Search the Archive

Re: Random Normal deviates within compiled function?

  • To: mathgroup at
  • Subject: [mg62302] Re: Random Normal deviates within compiled function?
  • From: John Doty <jpd at>
  • Date: Sat, 19 Nov 2005 23:18:29 -0500 (EST)
  • References: <dln17e$gf4$>
  • Sender: owner-wri-mathgroup at

Gareth Russell wrote:

> Hi All,
> I have a function which needs to be quick as it's used in simulations, 
> and it requires the internal generation of a large vector of random 
> normal deviates (up to 50,000). It appears that a function like 
> Random[NormalDistribution[0,s]] cannot be compiled. Can anyone suggest 
> an algorithm for getting such numbers that would work within a Compile 
> statement and still be quicker than using the non-compiled function?
> The non-compiled function would look as follows:
> f[v_,r_,k_,s_,q_]:=Select[v*Exp[r*(1 - v/k) +
>        RandomArray[NormalDistribution[0, s],
>           Length[v]]], # > q &]
> where v is a vector of 50,000 reals, and r, k, s and q are scalar reals.
> (If anyone is interested, this is for population projection in 
> population viability analysis.)
> Thanks,
> Gareth Russell

I've been using the following:

fastNoise = Compile[{}, 2.(Random[] + Random[] + Random[] - 1.5)];

It makes variates with mean 0, variance 1, with a roughly normal 
distribution (for your problem multiply by s to adjust the variance). 
Whether this is accurate enough depends on the problem: its main defect 
is that its PDF falls to 0 outside the range [-3,3], so it won't 
generate the rare extreme values in the normal distribution. Whether 
this is a problem depends on what you're trying to calculate.


  • Prev by Date: Re: Types in Mathematica
  • Next by Date: Re: Same scaling for plots/ charts
  • Previous by thread: Re: Confusing results with N[expr]?
  • Next by thread: Re: Random Normal deviates within compiled function?