MathGroup Archive 2010

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

Search the Archive

Re: Easy question, please help to run a function n times

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109641] Re: Easy question, please help to run a function n times
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 9 May 2010 07:51:22 -0400 (EDT)

On 5/8/10 at 7:08 AM, hays.joe at gmail.com (Joe Hays) wrote:

>I would use a FOR loop and collect the generated points in a
>preallocated vector like,

>M=74;
>n=88;
>T = Range[10000];
>For[i=1, i<=10000, i++,
>R= RandomReal[{15,23}];
>T[[i]]= M/(R/n);
>];
>myMean = Mean[T];

>However, this is very much a procedural programming approach. I'm
>sure the Mathematica experts helping on this list can provide a more
>efficient approach...

One thing about efficiency.

Doing:

myMean = Mean[M/(RandomReal[{15,23}, 10000]/n)]

is far more efficient than the loop in terms of execution speed.
But the For loop, is far more efficient in terms of memory used
since the loop does not need memory for a large array.



  • Prev by Date: Re: How to Explain This Behavior of Simple Procedures?
  • Next by Date: Re: How to write reports and books in Mathematica
  • Previous by thread: Re: Easy question, please help to run a function n times
  • Next by thread: labels in GraphicsGrid[]