Re: Troubling Bug involving RandomReal, NestList, and Table
- To: mathgroup at smc.vnet.net
- Subject: [mg86368] Re: Troubling Bug involving RandomReal, NestList, and Table
- From: "jwmerrill at gmail.com" <jwmerrill at gmail.com>
- Date: Sun, 9 Mar 2008 05:05:50 -0500 (EST)
- References: <fqo8tn$t2o$1@smc.vnet.net> <fqqrvv$kcl$1@smc.vnet.net>
On Mar 7, 2:48 am, Szabolcs Horv=E1t <szhor... at gmail.com> wrote: > jwmerr... at gmail.com wrote: > > When making tables of 250 or more directions, Mathematica seems to > > forget how to do arithmetic. I realize these "random" directions are > > not properly uniform, but that's beside the point. > > randomDirection3[] := > > NestList[Sqrt[1 - #^2] &, RandomReal[], 1] > > Variance[Table[Norm[randomDirection3[]], {249}]] > > Variance[Table[Norm[randomDirection3[]], {250}]] > > >> 1.49104*10^-33 > >> 0.0513843 > One workaround that you could use (without giving up compilation) is this:= > > cp = Compile[{}, With[{r = Random[]}, {r, Sqrt[1 - r^2]}]] > > (But I am sure you already found many other ways around the problem.) > > Szabolcs Thanks to all for your explanations and advice. My eventual solution (pilfered from elsewhere on the internet), was randomUnitVector[dimensions_] := Normalize[RandomReal[NormalDistribution[], dimensions]] Regards, JM