Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Troubling Bug involving RandomReal, NestList, and Table

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86273] Re: [mg86236] Troubling Bug involving RandomReal, NestList, and Table
  • From: Darren Glosemeyer <darreng at wolfram.com>
  • Date: Fri, 7 Mar 2008 02:29:35 -0500 (EST)
  • References: <200803060802.DAA29370@smc.vnet.net>

jwmerrill 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.
>
> $Version
>   
>> "6.0 for Mac OS X x86 (32-bit) (April 20, 2007)"
>>     
>
> randomDirection3[] :=
>  NestList[Sqrt[1 - #^2] &, RandomReal[], 1]
>
> ListPlot[Table[randomDirection3[], {249}], AspectRatio -> Automatic]
> ListPlot[Table[randomDirection3[], {250}], AspectRatio -> Automatic]
>
>   
>> [Graphs you'll need to generate for yourself]
>>     
>
> Variance[Table[Norm[randomDirection3[]], {249}]]
> Variance[Table[Norm[randomDirection3[]], {250}]]
>
>   
>> 1.49104*10^-33
>> 0.0513843
>>     
>
> What!?!
>
> Jason Merrill
>   

This appears to be Table compilation problem. Note that problems occur 
when the table size hits the default length for compiling tables.

In[1]:= "TableCompileLength" /. ("CompileOptions" /. 
Developer`SystemOptions[])

Out[1]= 250

I've passed this along to the appropriate people in the company so it 
can be investigated further.

One possible solution would be to increase the value of 
"TableCompileLength" (I'll show this below), but I would suggest instead 
writing a function to get n values which gets all RandomReal values in 
one call. This avoids the problem and will be faster because only one 
call to RandomReal is needed to get n random values.


In[2]:= randomDirection[n_] :=
         Map[NestList[Sqrt[1 - #^2] &, #, 1] &, RandomReal[1, n]]

In[3]:= Timing[randomDirection[10^6];]

Out[3]= {1.984, Null}


The following shows the approach of increasing the value of 
"TableCompileLength" and obtaining values via a Table of 
randomDirection3[] values.

In[4]:= Developer`SetSystemOptions[
          "CompileOptions" -> {"TableCompileLength" -> Infinity}];

In[5]:= randomDirection3[] := NestList[Sqrt[1 - #^2] &, RandomReal[], 1]

In[6]:= Timing[Table[randomDirection3[], {10^6}];]

Out[6]= {7.047, Null}

Plots for points from both of the approaches above appear as expected.


Darren Glosemeyer
Wolfram Research


  • Prev by Date: Re: Troubling Bug involving RandomReal, NestList, and Table
  • Next by Date: New version 4.1 of BIOKMOD for MATHEMATICA 6 is available
  • Previous by thread: Troubling Bug involving RandomReal, NestList, and Table
  • Next by thread: Re: Troubling Bug involving RandomReal, NestList, and Table