MathGroup Archive 2000

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

Search the Archive

Re: Extended Random Function

Daniele wrote:

> I have a question concerning the use of the
> extension of the Random[] function as defined in
> the Statistics`ContinuousDistributions`. With
> this extension it is possible to pick a random
> number according to a given distribution function.
> My question is: how is it possible to pick a Real
> number with n digits? With the default function
> it is possible to do so by calling
> Random[Real,{xmin,xmax},ndigits]

If you look through the code in Statistics`ContinuousDistributions` or
Statistics`NormalDistribution` you will see that machine-precision is
used. E.g.,

  normal = Compile[{{mu, _Real}, {sigma, _Real}, {q1, _Real}, {q2,
    mu + sigma Sqrt[-2 Log[q1]] Cos[2Pi q2] ]

  NormalDistribution/: Random[NormalDistribution[mu_:0, sigma_:1]] :=
    normal[mu, sigma, Random[], Random[]]

and there is no option for specifying n digits. Note also that Compile
requires the
use of machine-precision.

You could modify such functions as follows:

  normal = Function[{mu, sigma, q1, q2}, mu + sigma Sqrt[-2 Log[q1]]
Cos[2Pi q2]];

replacing Compile with Function and removing the "argument typing",

  NormalDistribution/: Random[NormalDistribution[mu_:0, sigma_:1],
ndigits_] :=
    normal[mu, sigma, Random[Real, {0, 1}, ndigits], Random[Real, {0,
1}, ndigits]]

adding an extra argument to NormalDistribution and modifying the calls
to Random.

For example,

  In[1]:= Random[NormalDistribution[1, 2], 25]
  Out[1]= 0.23230352490529443816318603637908270412


  • Prev by Date: Re: Could this be improved?
  • Next by Date: Re: NoteBook Styles in 4
  • Previous by thread: Re: Extended Random Function
  • Next by thread: More about "mma" becoming "Mathematica"